I need to run a php script as daemon process (wait for instructions and do stuff). cron job will not do it for me because actions need to be taken as soon as instruction arrives. I know PHP is not really the best option for daemon processes due to memory management issues, but due to various reasons I have to use PHP in this case. I came across a tool by libslack called Daemon (http://libslack.org/daemon) it seems to help me manage daemon processes, but there hasn't been any updates in the last 5 years, so I wonder if you know some other alternatives suitable for my case. Any information will be really appreciated.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
With new systemd you can create a service (on rhel based linux).
You must create a file or a symlink in
/etc/systemd/system/
, eg. myphpdaemon.service and place a content like this one, myphpdaemon will be the name of the service:You will be able to start, get status, restart and stop the services using the command
systemctl <start|status|restart|stop|enable> myphpdaemon
The PHP script should have a kind of "loop" to continue running.
Working example:
If your PHP should be executed once in a cycle (like a diggest) you may should use a shell or bash script to be invoked into systemd service file instead of PHP directly, for example:
If you chose these option you should change the KillMode to
mixed
to processes, bash(main) and php(child) be killed.You can
nohup
as Henrik suggested.screen
and run your PHP program as a regular process inside that. This gives you more control than usingnohup
.I'd recommend the simplest method (screen in my opinion) and then if you want more features or functionality, move to more complex methods.
Check out https://github.com/shaneharter/PHP-Daemon
This is an object-oriented daemon library. It has built-in support for things like logging and error recovery, and it has support for creating background workers.
you can check pm2 here is, http://pm2.keymetrics.io/
create a ssh file, such as worker.sh put into your php script that you will deal with.
worker.sh
daemon start
Cheers, that is it.
If you can - grab a copy of Advanced Programming in the UNIX Environment. The entire chapter 13 is devoted to daemon programming. Examples are in C, but all the function you need have wrappers in PHP (basically the pcntl and posix extensions).
In a few words - writing a daemon (this is posible only on *nix based OS-es - Windows uses services) is like this:
umask(0)
to prevent permission issues.fork()
and have the parent exit.setsid()
.SIGHUP
(usually this is ignored or used to signal the daemon to reload its configuration) andSIGTERM
(to tell the process to exit gracefully).fork()
again and have the parent exit.chdir()
.fclose()
stdin
,stdout
andstderr
and don't write to them. The corrrect way is to redirect those to either/dev/null
or a file, but I couldn't find a way to do it in PHP. It is possible when you launch the daemon to redirect them using the shell (you'll have to find out yourself how to do that, I don't know :).Also, since you are using PHP, be careful for cyclic references, since the PHP garbage collector, prior to PHP 5.3, has no way of collecting those references and the process will memory leak, until it eventually crashes.
Extending Emil Ivaov answer, You can do the following to close STDIN, STDOUT AND STDERROR in php
Basically you close the standard streams so that PHP has no place to write. The following
fopen
calls will set the standard IO to/dev/null
.I have read this from book of Rob Aley - PHP beyond the web