PHP can also directly start and terminate the daemon process. Compared with the shell, it is much simpler and more convenient to understand. Of course, if the php daemon needs to automatically restart, it still depends on the shell's crontab schedule. Execute the script every once in a while to see if 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.
The code copy 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 number in the runfile can match the running one, and the difference between the last time you access the runfile and the current difference is less than 5 minutes, then return
if(($oldpid == $nowpid) && (time() - fileatime($runfile) < 300)){
echo "$file is circle running no";
return;
}else{
//The pid number does not match or the loop statement has not been run for 300 seconds. Just 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){
//Receive a new end process number, end the process, and delete the relevant files
if(file_exists($diefile) && unlink($runfile) && unlink($diefile)){
return;
}
/*This is what the daemon needs to do*/
file_put_contents($file,"I'm Running Now".PHP_EOL,FILE_APPEND);
/************************/
touch($runfile);
sleep(5);
}
}
}
start("test");
Some points to pay attention to when writing daemons with hp:
1. The first thing is the function clearstatcache() function. Looking at the official manual, you can know that the function clears the file status cache. When checking the cache status of the same file multiple times in a script, an error will occur if the function is not used. The affected functions are: 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 detected before running. The time distance of the last loop is now greater than 300s or the pid number does not match will be restarted (the time must be updated in each loop execution).
3. Automatic restart also uses crontab's schedule, add the file to the schedule:
The code copy is as follows:
crontab -e
#Open the schedule, inset mode
*/3 * * * * /usr/bin/php -f process.php
#Execute every 3 minutes, put the process to hang
This is basically OK. If there are specific functions, you need to change the code.