PHP daemon instance and php daemon
Php can also directly start and terminate the daemon process, which is much simpler than shell and easier to understand, of course, the php daemon should depend on the shell crontab calendar for automatic restart. Execute a script at intervals to check whether the script needs to be restarted. If necessary, kill the process and delete the RunFile file, restart and write the pid in the RunFile file.
Copy codeThe Code is as follows:
<? Php
Function start ($ file ){
$ Path = dirname (_ FILE __).'/';
$ Runfile = $ path. $ file. '. run ';
$ Diefile = $ path. $ file. '. die ';
$ File = $ path. "data/{$ file}. php ";
Clearstatcache ();
If (file_exists ($ runfile )){
$ Oldpid = file_get_contents ($ runfile );
$ Nowpid = shell_exec ("ps aux | grep 'php-f process. php' | grep $ {oldpid} | awk '{print $2 }'");
// If the pid in runfile can match the running ID, and the time difference between the last access to runfile and the current time is less than 5 minutes, the system returns
If ($ oldpid ==$ nowpid) & (time ()-fileatime ($ runfile) <300 )){
Echo "$ file is circle runing no ";
Return;
} Else {
// If the pid number does not match or the loop statement has not been run for 300 seconds, kill the process and restart it.
$ Pid = file_get_contents ($ runfile );
Shell_exec ("ps aux | grep 'php-f process. php' | grep {$ pid} | xargs -- if-no-run-empty kill ");
}
} Else {
// Write the file pid to the run file
If (! ($ Newpid = getmypid () |! File_put_contents ($ runfile, $ newpid )){
Return;
}
While (true ){
// Receives the new ending process number, ends the process, and deletes the relevant files.
If (file_exists ($ diefile) & unlink ($ runfile) & unlink ($ diefile )){
Return;
}
/* Here is what the daemon is doing */
File_put_contents ($ file, "I'm Runing Now". PHP_EOL, FILE_APPEND );
/***********************/
Touch ($ runfile );
Sleep (5 );
}
}
}
Start ("test ");
Note the following points when writing a daemon to hp:
1. the first is the clearstatcache () function. You can check the official manual to find that the function clears the File status cache, when the cache status of the same file is checked multiple times in a script, if this function is not used, an error will occur. The affected functions include stat (), lstat (), file_exists (), is_writable (), is_readable (), is_executable (), is_file (), is_dir (), is_link (), filectime (), fileatime (), filemtime (), fileinode (), filegroup (), fileowner (), filesize (), filetype (), fileperms ().
2. when the script is run multiple times, it will be checked before running, the process is restarted when the time of the last loop is greater than s or the pid does not match (touch must be updated at each execution cycle ).
3. The crontab calendar is also used for automatic restart. Add the file to the calendar:
Copy codeThe Code is as follows:
Crontab-e
# Enable calendar in inset Mode
*/3 */usr/bin/php-f process. php
# Run the command once every 3 minutes and put the process down
In this way, the code needs to be modified if specific functions are available.