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.
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:
*/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.