Home > Web Front-end > JS Tutorial > PHP Daemon Example_Basic Knowledge

PHP Daemon Example_Basic Knowledge

WBOY
Release: 2016-05-16 16:10:51
Original
1143 people have browsed it

PHP can also directly start and terminate the daemon process. Compared with the shell, it is much simpler and easier to understand. Of course, the automatic restart of the php daemon process still depends on the crontab schedule of the shell. Execute the script every once in a while to see if the script needs to be restarted. If necessary, kill the process, delete the RunFile file, restart and write the pid in the RunFile file.

Copy code The code is as follows:

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 the runfile was accessed and the current time is less than 5 minutes, then return
If(($oldpid == $nowpid) && (time() - fileatime($runfile) < 300)){
echo "$file is circle running no";
             return;
         }else{
//If the pid number does not match or the loop statement has not been run for 300 seconds, kill the process directly and restart
                $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 into the run file
If(!($newpid = getmypid()) || !file_put_contents($runfile,$newpid)){
             return;
}
         while(true){
//Receive the new number of the end process, end the process, and delete related files
If(file_exists($diefile) && unlink($runfile) && unlink($diefile)){
                  return;
            }
                 /*Here is what the daemon process does*/
              file_put_contents($file,"I'm Running Now".PHP_EOL,FILE_APPEND);
               /***********************/
               touch($runfile);
              sleep(5);
}
}
}
start("test");

Hp should pay attention to a few points when writing daemon processes:

1. The first is the function clearstatcache(). Check the official manual to know that this 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 this function is not used. , those affected by this function 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. If the time since the last execution of the loop is now greater than 300s or the pid number does not match, the process will be restarted (the time must be updated every time the loop is executed).
3. Automatic restart also uses the crontab schedule. Add this file to the schedule:

Copy code The code is as follows:

crontab -e
#Open schedule, inset mode

*/3 * * * * /usr/bin/php -f process.php
#Execute once every 3 minutes, put the process to hang

This is basically ok. If there are specific functions, the code still needs to be modified.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template