Using the shell to combine awk is a good way to tell if a process is running or if a program file is being executed, as an example of PHP.
Define a crontab, execute every minute, the time interval is one minute, possibly next time executes, the last time did not finish, therefore we must make the judgment.
1,test.php in the case of running, view process
# Ps-ef |grep test.php
Root 29884 29881 0 10:48? 00:00:00 awk nr==1{\?if ($ = = "grep") {\?system ("Cd/home/test/cron && php test.php"); \?} Else{\?print "Running"; \?} \?}
Root 29888 29884 0 10:48? 00:00:00 sh-c cd/home/test/cron && php test.php
Root 29889 29888 1 10:48? 00:00:00 PHP test.php
Root 30265 7620 0 10:48 pts/0 00:00:00 grep test.php
2,test.php is not running, as follows
# Ps-ef |grep test.php
Root 30265 7620 0 10:48 pts/0 00:00:00 grep test.php
The 3,test.sh script is as follows
View copy print?
# Cat Test.sh
Ps-ef|grep test.php | awk ' nr==1{\
if ($ = = "grep") {\
System ("Cd/home/test/cron && php test.php"); \
}else{\
print "Running"; \
}\
}'
Exit
Ps-ef|grep test.php, when the 8th column equals grep, test.php is not running, system calls PHP to execute test.php, and vice versa.
4, the actual operation of the situation
After the execution of the test.sh, the day is similar, and there will be one such case,
# Ps-ef |grep test.php
Root 8043 8040 0 09:33? 00:00:00 awk nr==1{\?if ($ = = "grep") {\?system ("Cd/home/test/cron && php test.php"); \?} Else{\?print "Running"; \?} \?}
Root 8044 8043 0 09:33? 00:00:00 sh-c cd/home/test/cron && php test.php
Root 8045 8044 0 09:33? 00:00:00 PHP test.php
Root 18952 18947 0 Oct14? 00:00:00 awk nr==1{\?if ($ = = "grep") {\?system ("Cd/home/test/cron && php test.php"); \?} Else{\?print "Running"; \?} \?}
Root 18954 18952 0 Oct14? 00:00:00 sh-c cd/home/test/cron && php test.php
Root 18955 18954 0 Oct14? 00:00:01 PHP test.php
Root 23430 23410 0 10:32 pts/0 00:00:00 grep test.php
Then cause the PHP program to die, has not been figuring out why this.
5, the solution
# cat t.sh
t=0
Eval $ (ps-ef |grep test.php |awk ' BEGIN {t = 0;} {if ($ = = "php") {t++}} End{print "t=" t;} ');
If [$t-gt 1]
then
kill-9 $ (ps-ef |grep test.php ' |awk ' {l[nr]=$2;a [Nr]=$8;} End{for (i=1;i<=nr;i++) {if (A[i]!= "grep") {print l[i];}} ');
else
echo "$t no Killing";
fi
exit;
t.sh meaning, every Time to check, test.php is being executed the number of times, if greater than 1, will force the end, all root test.php related processes.
6,awk View, inverted sort view, server various processes
View replication printing?
# ps-ef| awk ' {a[$8]++}end{for (i in a) {print i,a[i] | "Sort-r-K 2"}} '