: This article mainly introduces the linux daemon. if you are interested in the PHP Tutorial, refer to it. Daemon is a special process running in the background. It is independent of the control terminal and periodically executes a task or waits
Process some events. Daemon is a very useful process.
Most Linux servers are implemented using Daemon. For example, the Internet server inetd and the Web server httpd.
At the same time, the Daemon completes many system tasks. For example, job planning process crond and printing process lpd.
The role of resident processes in Linux cannot be ignored, but the problems here cannot be ignored. it is reasonable to design how to start the process, how to end the process, and how to restart the process after the process fails. The following is an example of a shell-controlled php resident process.
It is easier to understand the following:
#!/bin/sh
# Filename test. sh # definitely locates the location of the file 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
# Determine if the RunFile file does not exist, it indicates 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, the program continues to run. Otherwise, the program enters else and exits/usr/bin/php-f $ {file} touch $ RunFile sleep 1 else.
# Clear RunFile and DieFile to exit if rm-rf $ RunFile & rm-rf $ DieFile; then exit fi done else # Here we try to start this process when RunFile exists
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 the same as the target process number in the running process, it indicates that everything is safe.
# If the process number in RunFile does not match the running target process, it indicates that the process is faulty. directly delete the RunFile and terminate 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 rm-f $ RunFile else echo $?> $ {Path}/data/error fi
There are only a few points to emphasize:
I use this shell to call the php program, which has no limitations. here I want to explain this method for running resident processes.
When RunFile exists, but the process number cannot be used to kill the process (that is, the place where the red else executes), be sure to 'grep-v $ ', the function is to filter out the currently running processes. otherwise, all processes will be killed and the subsequent processes will not be executed.
Another thing to note is about automatic restart.
Automatic restart can be placed in the crontab and executed every other time.
Crontab-e # open the current user's calendar, add mode # There are 5 asterisks in the calendar, f1, f2, f3, f4, f5,
# F1 indicates the minute, f2 indicates the day, f3 indicates the day, f4 indicates the month, and f5 indicates the day of the week.
# * Indicates every minute/hour/day/month/week, */n indicates every n minutes/hour /...... run one */2 */root/test. sh # run once every 2 minutes
The programming of the Daemon is not complex. the complicated problem is that the implementation mechanisms of Unix in different versions are different,
The programming rules of daemon in different Unix environments are inconsistent.
It should be noted that copying the rules in some books (especially BSD4.3 and earlier versions of System V) will cause errors to Linux.
The above is the content of the daemon process in linux. For more information, see PHP Chinese network (www.php1.cn )!