PHP Implementation Daemon Start and terminate operations

Source: Internet
Author: User
PHP is also able to start and terminate the daemon directly, compared to the shell will be much easier to understand more convenient, of course, the PHP daemon to achieve automatic restart or to rely on the Shell's crontab schedule, every time to execute a script to see if the script needs to restart, If necessary, kill the process to delete the Runfile file, reboot and write the PID in the Runfile file.

<?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 $} '); If the PID number in the Runfile can match to the running, and the time of the last access to runfile and the current difference is less than 5min then return if (($oldpid = = $nowpid) && (Time ()-Fileatime ($            (Runfile) <) {echo "$file is Circle runing no";        Return            }else{//pid does not match or has 300 seconds without running the loop statement, directly kill the process, 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 to the run file if (!) ( $newpid = Getmypid ()) | |        !file_put_contents ($runfile, $newpid)) {return; } while (true) {//receives the end process new number, ends the process,and delete the relevant file if (File_exists ($diefile) && unlink ($runfile) && unlink ($diefile)) {retur            N }/* Here is what the daemon will do */file_put_contents ($file, "I m runing Now".            Php_eol,file_append);            /***********************/Touch ($runfile);        Sleep (5); }}}start ("Test");

some points to note when PHP writes the daemon:

1. First is the function Clearstatcache () function There, check the official manual can know that the function is to clear the file state cache, when the same file in a script to check the state of the cache multiple times if you do not use the function will be an error, affected by the function is: 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 is instrumented before running, and the last execution cycle is now greater than 300s or the PID number mismatch restarts the process (the time is updated with touch on each execution loop).

3. The automatic restart also uses the Crontab schedule to add the file to the timeline:

Crontab-e

#打开日程表, inset mode

*/3 * * * */usr/bin/php-f process.php

#每3分钟执行一次, the placement process hangs out

This is basically OK, if there is a specific function, you need to change the code.

  • Related Article

    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.