Enable and terminate the daemon in php

Source: Internet
Author: User
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:


# 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.