通过flag文件来检测与否在运行。
可以加入相关关键字的进程的检测。
代码:(简单的csh实例)running_check
#!/bin/csh -f
if ($#argv < 2 ) then
usage:
echo "Usage: $0 <running_flag> <action> <timeout>"
echo "action need be one value of -check or -delete."
echo "default timeout is 0, it means no timeout limitation."
exit 1
endif
set running_flag = $1
set action = $2
set timeout = 0
if ( "$3" != "") set timeout = $3
if ( "$action" != "-check" && "$action" != "-delete" ) goto usage
# wait until the flag is deleted by ohter processes or timeout.
set waittime = 0
if ("$action" == "-check" && -e $running_flag ) then
echo "running flag is existed, please wait!"
set running = 1
set sleeptime = 60
while(("$timeout" != "0" && $waittime < $timeout) && $running \
|| "$timeout" == "0" && $running)
echo "sleep $sleeptime" && sleep $sleeptime
@ waittime += $sleeptime
if (! -e $running_flag) set running=0
end
endif
# timeout, delete the flag.
if ("$timeout" != "0" && "$waittime" >= "$timeout") then
echo "timeout $timeout"
echo "delete running flag"
if ( -e $running_flag) rm $running_flag
endif
# if check, set new flag.
# if delete, delete the flag.
if ( "$action" == "-check" ) then
echo "touch running flag"
touch $running_flag
echo `date` > $running_flag
ls -l $running_flag
else
echo "delete running flag"
if ( -e $running_flag) rm $running_flag
endif
exit 0
if ($#argv < 2 ) then
usage:
echo "Usage: $0 <running_flag> <action> <timeout>"
echo "action need be one value of -check or -delete."
echo "default timeout is 0, it means no timeout limitation."
exit 1
endif
set running_flag = $1
set action = $2
set timeout = 0
if ( "$3" != "") set timeout = $3
if ( "$action" != "-check" && "$action" != "-delete" ) goto usage
# wait until the flag is deleted by ohter processes or timeout.
set waittime = 0
if ("$action" == "-check" && -e $running_flag ) then
echo "running flag is existed, please wait!"
set running = 1
set sleeptime = 60
while(("$timeout" != "0" && $waittime < $timeout) && $running \
|| "$timeout" == "0" && $running)
echo "sleep $sleeptime" && sleep $sleeptime
@ waittime += $sleeptime
if (! -e $running_flag) set running=0
end
endif
# timeout, delete the flag.
if ("$timeout" != "0" && "$waittime" >= "$timeout") then
echo "timeout $timeout"
echo "delete running flag"
if ( -e $running_flag) rm $running_flag
endif
# if check, set new flag.
# if delete, delete the flag.
if ( "$action" == "-check" ) then
echo "touch running flag"
touch $running_flag
echo `date` > $running_flag
ls -l $running_flag
else
echo "delete running flag"
if ( -e $running_flag) rm $running_flag
endif
exit 0
完!