When you write a shell script that starts with the Crontab timer task, if the shell script execution time exceeds, Crontab pulls the script time interval, this time may appear the same script simultaneously initiates several times the phenomenon. This time may lead to duplication of data collection, calculation inaccuracies and so on, in such a clear first and sequence requirements, there will be problems.
Some means must be taken to prevent this phenomenon. It is possible to use "$$" and "$" to achieve this requirement, only after the execution of the current script is completed, the second execution can be initiated. Okay, no more nonsense, just paste a demo, as long as a little change, you can apply to their own program inside. With the same flexibility, you can achieve the effect of the daemon.
$ $ Current Program name $n Current program parameters (starting at 1, space-delimited) $* all parameters of the current program $# the current program's parameters $$ the current program's PID (the current process ID number of the script run) $? Executes the return value of the previous instruction (displays the exit status of the last command. 0 means no error, any other value indicates an error)
First time execution
#sh ponlyone.sh &[1] 30538# pid=30538 pname=ponlyone.sh expid=30452$$==30538$0==ponlyone.sh$?==0$#==1$1== 111$2==$\!==
Second execution
#sh ponlyone.sh &[2] 30570# pid=30570 pname=ponlyone.sh expid=30538the process ponlyone.sh is already exists! PID : 30538[2]+ done sh ponlyone.sh
#!/bin/shsource/etc/profilepid= ' echo $$ ' pname= ' echo $ ' mypidfile=~/pid.testecho-e ' \ n pid= $pid pname= $pname \ n ' Function Dodo () { echo "\$\$==" $$ echo "\$0==" $ echo "\$?==" $? echo "\$#==" $# echo "\$1==" echo "\$2==" $ echo "\$\!==" $! Sleep 100}if test-f "$mypidfile" then expid= ' cat $mypidfile ' echo ' expid= $expid " pfalg= ' Ps-ef|grep" $ Expid "|grep" $pname "|wc-l" If ["1" = "$pfalg"] then echoes "the process $pname is already exists! PID: $expid " else echo $pid > $mypidfile Dodo 111 fielse echo $pid > $mypidfile Dodo fi
Copyright NOTICE: Disclaimer: This document can be reproduced, must be signed by the original author. Inaction qq:490073687
Shell Tips--use "$$" and "$" to prevent the same script from starting multiple times