linux - Managing php daemon -
how can manage daemon web without changing simple cli runtime php-fpm?
the daemon starts automatically after os starts , works cli-application without php-fpm pool. basic ideology of daemon eco-system work without php-fpm pool (cli-sapi).
server configuration:
- debian 7
- apache 2.2
- php5-fpm (v 5.4.35) — mod_fastcgi
- daemon.php
- daemon_manager.php — management script start|stop|restart|kill daemon.php command line.
- daemon_manager_web.php — administrator script managing daemon browser.
daemon.php regular php daemon this:
<?php declare(ticks=1); ini_set("max_execution_time", "0"); ini_set("max_input_time", "0"); set_time_limit(0); /* catching signals */ function sig_handler($signo) { switch ($signo) { case sigquit: case sigterm: // work pcntl_wait($status); break; //... } } pcntl_signal(sigterm, 'signal_handler'); pcntl_signal(sigquit, 'signal_handler'); $newpid = pcntl_fork(); if ($newpid == -1) { throw new exception('cannot fork porcess'); } elseif ($newpid) { print "starting daemon under pid=$newpid\n"; // ... exit; }
the problem.
since pcntl-functions not available web, manage daemon functions such exec(), shell_exec(). when stop , start daemon again using daemon_manager_web.php browser starts works under php-fpm pool.
processes list before restart:
$ ps aux | grep php root 5952 0.0 2.9 69008 14952 pts/0 s 14:24 0:00 php /var/www/daemon.php $ service php5-fpm status php5-fpm.service - lsb: starts php5-fpm loaded: loaded (/etc/init.d/php5-fpm) active: active (running) since fri, 05 dec 2014 11:28:25 +0200; 11h ago process: 1003 execstart=/etc/init.d/php5-fpm start (code=exited, status=0/success) cgroup: name=systemd:/system/php5-fpm.service ├ 1627 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) ├ 9562 php-fpm: pool www ├ 9605 php-fpm: pool www └ 9633 php-fpm: pool www
processes list after restart browser:
$ service php5-fpm status php5-fpm.service - lsb: starts php5-fpm loaded: loaded (/etc/init.d/php5-fpm) active: active (running) since fri, 05 dec 2014 11:28:25 +0200; 11h ago process: 1003 execstart=/etc/init.d/php5-fpm start (code=exited, status=0/success) cgroup: name=systemd:/system/php5-fpm.service ├ 1627 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) ├ 4987 php-fpm: pool www ├ 5040 php-fpm: pool www ├ 9432 php-fpm: pool www └ 9492 /usr/bin/php /var/www/daemon.php
you should not start daemon via apache in way. proper way start daemon such (managed by, say, supervisord, have pretty track record using in production) , opening file socket (af_unix) on socket_select()
, idle-wait input triggers processing. way, "interface" (on apache) connects sockets , writes it.
on topic, i've found writing daemons in php quite tedious , might want choose library handle nitty-gritty (cannot recommend any) or tool more suitable event loop typical of daemons (node.js?)
Comments
Post a Comment