Home > Article > Operation and Maintenance > Linux daemon process example code analysis
The role of the resident process under Linux cannot be ignored, but the problems within it cannot be ignored either. How to start the process, how to end the process, and how to restart the process after the process hangs up must be designed reasonably. Let's look at an example of a shell-controlled PHP resident process.
Copy code The code is as follows:
#!/bin/sh
#filename test.sh
#The location of the file is absolutely positioned and does not change with the execution directory
cd $(cd "$(dirname "$0")";pwd)
readonly path=$(pwd)/
file=$1;
runfile="${path}data/${file} .run"
diefile="${path}data/${file}.die"
readonly file="${path}${file}.php"
if [ ! -f "$ file" ]; then
echo "please select a exists file"
elif [ ! -f "$runfile" ]; then
#Judge here. If the runfile file does not exist, it means that the process does not exist. , start the process below
echo $$>${runfile}
while true
do
if [ ! -f $diefile ]; then
If the diefile file does not exist, then Indicates that the program continues to execute, otherwise it enters else and performs the exit operation Clear runfile and diefile to exit
if rm -rf $runfile && rm -rf $diefile ; then
exit
fi
fi
else
# Here is existence Runfile is used to try to start the process
oldpid=`cat $runfile`
newpid=`ps aux | grep "process.sh $1" | grep -v grep | grep "$oldpid" | awk '{print $2}'`
if [[ $oldpid -eq $newpid ]]; then
#If the process number in the runfile is consistent with the running target process number, everything is fine^_^
echo "the process is running now"
exit
else
#If the process number in the runfile cannot match the running target process, it means there is a problem with the process. Delete the runfile directly and end the running process
echo "error situation, kill the run process and delete the run file"
ps aux | grep "process.sh $1" | grep -v 'grep' | awk '{print $2}' | grep -v $ $ | xargs --no-run-if-empty kill
If [ $? -eq 0 ]; then
/data/error
There are no limitations to this. What I want to explain here is this method of running a resident process.
When the runfile exists, but the process number does not match the killing process (that is, where the red else is executed), you must `grep -v $$`, the function is to filter out the currently running processes, otherwise they will be killed, and the subsequent ones will not be executed.
Another thing to note is about automatic restart
Automatic restart It can be placed in the crontab and executed every once in a while. The specific situation will be treated on a case-by-case basis.
Copy code The code is as follows:
crontab -e
#Open the current user schedule and add mode
#There are 5 asterisks in the schedule, f1, f2, f3, f4, f5,
#where f1 represents minute, f2 represents hour, f3 represents day, f4 represents month, f5 represents the day of the week
#* represents every minute/hour/day/month/day of the week, */n means to execute once every n minutes/hours/......
#Execute once every 2 minutes
Such a complete resident process function is completed. If you want to terminate the process, you only need to touch ${diefile} in the corresponding directory.
The above is the detailed content of Linux daemon process example code analysis. For more information, please follow other related articles on the PHP Chinese website!