Whether the shell awk checker in Linux executes

Source: Internet
Author: User
Tags time interval

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"}} '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.