Forker allows the php-cli process to run as a daemon with nohup.

Source: Internet
Author: User
Forker allows the php-cli process to run as a daemon with nohup. This Forker only makes the process a daemon and does not copy the memory of the parent process. None? Php runs the instance and fork10 processes. Each process Outputs An Imaworker line and stores it in tmpforker. log: CLI command: phpForker. php10

Forker allows the php-cli process to run as a daemon with nohup. This Forker only makes the process a daemon and does not copy the memory of the parent process. None? Php // run the instance and fork 10 processes. Each process outputs a line of Im a worker and stores it in/tmp/forker. log: // CLI command: php Forker. php 10/

Forker allows the php-cli process to run as a daemon with nohup. This Forker only makes the process a daemon and does not copy the memory of the parent process. <无>
  Fork ($ forker-> findCommand ('php'). ''. _ FILE __, (int) $ argv [1] <= 0? 10: (int) $ argv [1]);} * // *** Forker allows the php-cli process to run in daemon mode with nohup. * This Forker only makes the process a daemon and does not copy the memory of the parent process. */Class Forker {private $ nohub = '/usr/bin/nohup'; private $ out ='/tmp/forker. log';/*** @ param string $ output file path. The standard output of the process will be redirected to this file * @ throws \ RuntimeException */public function _ construct ($ output = '') {if (false! ==( $ Nohup = $ this-> findCommand ('nohup') {$ this-> nohub = $ nohup;} if (! Is_executable ($ this-> nohub) {throw new \ RuntimeException ('nohup not excutable ');} if ($ output) {$ this-> setOutput ($ output) ;}/ *** sets the path of the output file. The standard output of the process will be redirected to this file * @ param string $ file * @ return \ Forker * @ throws \ RuntimeException */public function setOutput ($ file) {if (! Is_file ($ file) {$ dir = dirname ($ file); if ((! Is_dir ($ dir )&&! Mkdir ($ dir, 0755, true) |! Is_writable ($ dir) {throw new \ RuntimeException ('output is not writable, can not create output') ;}} else if (! Is_writable ($ file) {throw new \ RuntimeException ('output is not writable');} $ this-> out = $ file; return $ this ;} /*** obtain the path of the output file * @ return string */public function getOutput () {return $ this-> out ;} /*** run the command * @ param string $ command. The file parameters in the command must use the absolute path * @ param int $ forks fork process count */public function fork ($ command, $ forks = 1) {for ($ I = 0; $ I <$ forks; ++ $ I) {$ this-> execute ($ command );}} /*** find the absolute path of the command according to the current environment * @ param string $ name * @ return boolean */public function findCommand ($ name) {$ file = trim (exec ("which {$ name}"); if (is_file ($ file) & is_executable ($ file) {return $ file ;} return false;}/*** run the command. If the command succeeds, true is returned. If the command fails, true is returned. False * @ param string $ command * @ return boolean */private function execute ($ command) {$ lines = []; $ code = 0; exec ("{$ this-> nohub} {$ command} >{$ this-> out} 2> & 1 &", $ lines, $ code ); if (0! ==( Int) $ code) {file_put_contents ($ this-> out, "fork {$ command} FAILD [{$ code}]: \ n ". implode ("\ n", $ lines ). "\ n", FILE_APPEND); return false;} file_put_contents ($ this-> out, "fork {$ command} OK \ n", FILE_APPEND); return true ;}}

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.