Linux process monitoring shell script code _linux Shell

Source: Internet
Author: User
Tags sleep

To ensure that the PHP page runs in the background, write a monitoring script, assuming that the program exits unexpectedly, you can automatically restart.

Shell Script/usr/local/scripts/receve.sh content:

#!/bin/bash
#funtion: Keep receve.php running
php= "/usr/local/php5/bin/php"
program=
"receve.php" #start Dameo
$PHP/home/httpd/$PROGRAM >>/usr/local/scripts/phpshell/receve.out &
chpid= "$!";
echo "$chpid" >/usr/local/scripts/phpshell/receve.sid
echo "Child pid Yes $chpid"
echo "status is $?
While [1]
does wait $chpid
exitstatus= "$?"
echo "Child pid= $chpid is gone, $exitstatus" >>/usr/local/scripts/phpshell/receve.php_error.log
echo "date ' >>/usr/local/scripts/phpshell/receve.php_error.log
echo ' ************************** ' >>/usr/ Local/scripts/phpshell/receve.php_error.log sleep
$PHP/home/httpd/$PROGRAM >>/usr/local/ Scripts/phpshell/receve.out &
chpid= "$!";
echo "$chpid" >/usr/local/scripts/phpshell/receve.sid
echo "next child pid Yes $chpid"
echo "Next status is $ ?"
Done

Need to ensure that the monitor script runs normally, automatically restarts the script/usr/local/scripts/restart_r.sh every morning, writes to Crontab:

#!/bin/bash
echo "" >/tmp/receve_pid.txt
/bin/ps Ax | /bin/grep "Receve.sh" | /bin/grep-v "grep" | /bin/awk ' {print $} ' >/tmp/receve_pid.txt
no1pid= ' cat/tmp/receve_pid.txt | sort-r ' no2pid= ' cat/usr/
Local/scripts/phpshell/receve.sid | Sort-r '
if [-Z $no 1pid];then
  echo "receve.sh pid is null, receve.sh are not running."
 else  
  /bin/kill-9 "$no 1pid"  
  echo "receve.sh killed"
fi sleep
3
If [-Z $no 2pid];then
  Echo receve.php pid is null and receve.php is not running.
 else
  /bin/kill-9 "$no 2pid"
  echo "receve.php killed"
fi
rm-rf/usr/local/scripts/phpshell/ Receve.sid Sleep 
3 
/usr/local/scripts/receve.sh >/dev/null &

Crontab

Copy Code code as follows:

#restart receve.sh
* * * */usr/local/scripts/restart_r.sh

Note: script naming should avoid duplication and avoid killing your own process.

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.