PHP can also directly start and terminate the daemon process, which is much simpler than shell and easier to understand. PHP can also directly start and terminate the daemon process, it 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 the script once every time to check whether the script needs to be restarted. if necessary, kill the process to delete the RunFile file, restart it, and write the pid in the RunFile file.
When writing a daemon in PHP, pay attention to the following points:
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:
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.