Transferred from: http://blog.csdn.net/cybertan/article/details/3235722
Transferred from: http://blog.sina.com.cn/s/blog_4c451e0e0100giqg.html
Scenario: Use a shell to see if a PHP program running in the background exits abnormally
If exiting, use the daemon to automatically restart.
MySQL-like safe_mysqld
A total of 3 programs
SHSTART.SH is responsible for initiating
sh.sh Daemon Main process
shstop.sh Safe Exit daemon and PHP program body
/**************************/
shstart.sh
#!/usr/local/bin/bash
/home/phpshell/sh.sh &
Shpid= "$!";
echo "$shpid" >/home/phpshell/sh.sid
/**************************/
sh.sh
#!/usr/local/bin/bash
php= "/usr/local/bin/php"
Program= "/www/time.php"
#start Dameo
$PHP $PROGRAM &
Chpid= "$!";
echo "$chpid" >/home/phpshell/php.sid
echo "Child pid is $chpid"
echo "Status is $?"
While [1]
Do
Wait $chpid
Exitstatus= "$?"
echo "Child pid= $chpid is gone, $exitstatus" >>/home/phpshell/phperror.log
echo ' Date ' >>/home/phpshell/phperror.log
echo "**************************" >>/home/phpshell/phperror.log
Sleep 10
$PHP $PROGRAM &
Chpid= "$!";
echo "$chpid" >/home/phpshell/php.sid
echo "Next child pid is $chpid"
echo "Next status is $?"
echo "Userkill is $userkill"
Done
/******************************/
shstop.sh
#!/usr/local/bin/bash
Chpid= "' Cat Sh.sid '";
Kill $chpid;
echo "Kill sh.sh done!"
PS ax|grep php |grep-v Grep|awk ' {print $} ' |xargs Kill
Sleep 2
PS ax|grep php |grep-v Grep|awk ' {print $} ' |xargs Kill
echo "Kill PHP Done"
Using this principle, we can realize many hanging wire problems of background program.
Daemons implemented by Shell scripts(2010-01-18 09:34:46)Reproduced▼
Tags: shell script daemon |
Category: *os FAQs |
# #! is not an annotation character, but rather specifies which interpreter the script executes.
# #! There is a space followed by a space followed by the full path of the interpreter and must be correct.
#! /bin/ash
Pro_path= ""
# Testpro is an executable program to be guarded, which guarantees it is always running
Program= "Testpro"
# This script keeps looping, while < conditions > and do on one line to add a semicolon after the condition
# keywords or commands such as if, then, while, and do are the beginning of a new expression,
# The expression before a new expression must end with a newline character or a semicolon (;)
# If the condition is not a single constant or variable but an expression, enclose it in [].
# while, until and for loops both start with do to form the loop body as done
While true; Do
# Take a 10-second break to ensure that the program to be nursed is up and running, depending on the situation
Sleep 10
# single quotes ' in the $ characters and \ Characters do not have a reference variable and escape function, but in the double quotation mark "" is possible!
# If there is a single quotation mark in the single quotation mark, all the single quotation marks will be removed when the output, and the contents of the single quotation mark are output as is.
# example: Echo ' has ' test '--and have test
# PS aux---A to show other user-initiated processes;
# U is the user name and time to display the START process;
# x is the process for displaying the system itself;
# PS aux | grep executable name--in the resulting all process information text that is currently started,
# filter text lines that contain the specified text (which executes the program name)
# Note: Suppose PS aux | The grep executable name has output, but the output is not a letter but two,
# A line of information for finding a text containing the specified text (which executes the program's name) (one line ending with a newline character 0x10)
# One for the grep executable name, you lose yourself too,
# This message is not needed because we just want to know if the executable program that specified the name started
# GREP-V Specifies text-to-output line text information that does not contain the specified text
# wc-l The number of rows in the output file (------and output line break statistics)
# PS aux | grep $PROGRAM | Grep-v grep | Wc-l---If a program with the specified program name starts, the result is greater than
pro_now= ' ps aux | grep $PROGRAM | Grep-v grep | Wc-l '
# Integer comparison:-lt, less than,-le-less than equals,-gt-greater than,-ge-greater than equals,-eq-equals,-ne-not equals
# if [condition] and then put on one line to add a semicolon after the condition
# If the number of currently specified programs starts is less than one
If [$PRO _now-lt 1]; Then
# 0-Standard input, 1-standard output, 2-> standard error message output
#/dev/null---Linux special files, it's like a bottomless pit, all the information redirected to it will disappear!
# 2 >/dev/null redirect stderr to/dev/null,1 >& 2-, redirect stdout to stderr,
# directly launches the specified program and does not display any output
# The executable program is followed by a space plus a &, indicating that the program to be executed is running in the background
./$PROGRAM 2>/dev/null 1>&2 &
# date >>./tinfo.log-Directional Output current datetime to file, add to end of file, if no file, create this file
Date >>./tinfo.log
# echo "Test Start" >>./tinfo.log--directional output test start added to end of file
echo "Test Start" >>./tinfo.log
# If Branch structure ends
Fi
# basically the same as above, is more than a grep t, the result is to filter out the information line containing T characters
#--and process stopped, D--and non-disruptive deep sleep, R--process running or ready, S---to receive signal sleep,
#---has completely died, Z--has completely terminated
Pro_stat= ' PS aux|grep $PROGRAM |grep t|grep-v grep|wc-l '
# If you specify a process with a status of stopped information greater than 0
If [$PRO _stat-gt 0]; Then
# Killall---to kill a process by name, -9--To send the program a signal value of 9, namely Sigkill (illegal hardware instructions)
# can also not specify a signal, the default is Sigterm, that is, the signal value is 15
Killall-9 $PROGRAM
Sleep 2
./$PROGRAM 2>/dev/null 1>&2 &
Date >>./tinfo.log
echo "Test Start" >>./tinfo.log
Fi
# while, until and for loops both start with do to form the loop body as done
Done
# exit is used to end the script and return the status value, 0-for success, not 0 for the error code, the value range is 0 ~ 255.
Exit 0
Daemons implemented by Shell scripts