A daemon (Daemon) is a special process that runs in the background. It is independent of the control terminal and periodically performs some kind of task or waits
Handle certain events that occur. Daemons are a very useful process.
Most Linux servers are implemented with daemons. For example, the Internet server Inetd,web server httpd and so on.
At the same time, the daemon completes many system tasks. For example, the job planning process Crond, the printing process lpd and so on.
The role of the resident process under Linux can not be ignored, but the problem can not be ignored, how to start the process, how to end the process, how to restart the process after the process is designed to be reasonable. Let's look at a shell-controlled example of a PHP resident process.
No nonsense, direct fishing dry, on the code, through the code to explain more easily understand:
#!/bin/sh
#filename test.sh #绝对定位该文件的位置, does not vary with the execution directory CD $ (CD "$ (dirname") ";p wd) 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
#这里进行判断如果RunFile文件不存在, the process does not exist and the process starts
Echo $$>${runfile} while True if [!-f $DieFile]; Then
#这里如果DieFile文件不存在, the program continues execution, otherwise enters else, performs an exit operation/usr/bin/php-f ${file} touch $RunFile sleep 1 Else
#如果DieFile文件存在清除RunFile和DieFile退出 if RM-RF $RunFile && rm-rf $DieFile; Then Exit fi fi do else #这里是在存在RunFile的情况下试图启动该进程
Oldpid= ' Cat $RunFile ' newpid= ' ps aux | grep "process.sh" | grep-v grep | grep "$oldpid" | awk ' {print $} ' if [[$oldpid-eq $newpid]]; Then
#如果RunFile中的进程号和正在运行的目标进程号一致, it means everything's okay. ^_^ echo "The process is runing now" exit else
#如果用RunFile中的进程号匹配不到正在运行的目标进程, it indicates that the process is having problems, directly deleting Runfile and ending the running process echo "error Situation,kill the run process and delete The run file " ps aux | grep" process.sh "| grep-v ' grep ' | awk ' {print $} ' | Grep-v $$ | xargs--no-run-if-empty Kill If [$?-eq 0]; then rm-f $RunFile else echo $?>${path}/data/error fi fi fi
There are only a few places to emphasize:
I use this shell to invoke the PHP program, this has no limitations, here is to explain this method of running the resident process
In the runfile exists, but the process number is not on the killing process (that is, where the red amount else executes), must be ' grep-v $$ ', the role is to filter out the current running process, or else are killed, will not execute the back
One more thing to watch out for is the automatic restart.
Automatic restart can be placed in the crontab, once every time, the specific situation of concrete treatment.
crontab-e# Open Current User schedule, add mode # There are 5 asterisks in the calendar, F1,f2,f3,f4,f5,
#其中f1表示分, F2, F3 represents the day, F4 represents the month, F5 represents one weeks the first day
#* means every minute/hour/day/month/Sunday, */n means every n/hour/... Execute Once */2 * * * */root/test.sh #没2分钟执行一次
The daemon programming itself is not complex, and the complexity is that different versions of Unix implement different mechanisms,
Programming rules that cause daemons in different UNIX environments are inconsistent.
It is important to note that copying some of the rules of the book (especially BSD4.3 and the lower version of System V) to Linux will cause errors.
The above is about the content of the daemon in Linux, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!