Php as service linux

Run php script as daemon process

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.

15 Answers 15

You could start your php script from the command line (i.e. bash) by using

the & puts your process in the background.

Edit:
Yes, there are some drawbacks, but not possible to control? That’s just wrong.
A simple kill processid will stop it. And it’s still the best and simplest solution.

If the terminal exists the process will NOT exit. That’s why the «nohup» command is there. I have been using a PHP script as daemon on all servers like just this for years now. There might be better solution, but this is the quickest one.

I agree with what’s been said here— this is a bad solution. You should create an init script for a couple of reasons: 1) Init script is launched automatically on startup 2) You can manage the daemon with start/stop/restart commands. Here is an example from servefault: serverfault.com/questions/229759/…

hey guys . it seems to me nohup and & does the same very thing: detaching the launched process from the current intance of the shell. Why do I need them both? Can I not just do php myscript.php & or nohup myscript.php ?? Thanks

If the script writes to stdout (via echo or var_dump) then you can catch this information with a log file like this: nohup php myscript.php > myscript.log &

Another option is to use Upstart. It was originally developed for Ubuntu (and comes packaged with it by default), but is intended to be suitable for all Linux distros.

This approach is similar to Supervisord and daemontools, in that it automatically starts the daemon on system boot and respawns on script completion.

How to set it up:

Create a new script file at /etc/init/myphpworker.conf . Here is an example:

# Info description "My PHP Worker" author "Jonathan" # Events start on startup stop on shutdown # Automatically respawn respawn respawn limit 20 5 # Run the script! # Note, in this example, if your PHP script returns # the string "ERROR", the daemon will stop itself. script [ $(exec /usr/bin/php -f /path/to/your/script.php) = 'ERROR' ] && ( stop; exit 1; ) end script 

Starting & stopping your daemon:

sudo service myphpworker start sudo service myphpworker stop 

Check if your daemon is running:

sudo service myphpworker status 

Thanks

A big thanks to Kevin van Zonneveld, where I learned this technique from.

Читайте также:  Установка ldap astra linux

loving this. Just wondering, is it possible to have multiple concurrent workers? I just have the problem that one worker isn’t enough any more.

Sudo «service myphpworker start» didn’t work for me. I used «sudo start myphpworker» and it works perfectly

@Pradeepta That’s because there is an error in the post — I’m not exactly sure what (and haven’t tested this), but I think that sudo service myphpworker start/stop/status only works with services that are in /etc/init.d not upstart services. @matt-sich seems to have uncovered the correct syntax. Another option is to use Gearman or Resque, which allows background processing & deamonization.

With new systemd you can create a service.

You must create a file or a symlink in /etc/systemd/system/ , eg. myphpdaemon.service and place content like this one, myphpdaemon will be the name of the service:

[Unit] Description=My PHP Daemon Service #May your script needs MySQL or other services to run, eg. MySQL Memcached Requires=mysqld.service memcached.service After=mysqld.service memcached.service [Service] User=root Type=simple TimeoutSec=0 PIDFile=/var/run/myphpdaemon.pid ExecStart=/usr/bin/php -f /srv/www/myphpdaemon.php arg1 arg2> /dev/null 2>/dev/null #ExecStop=/bin/kill -HUP $MAINPID #It's the default you can change whats happens on stop command #ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s StandardOutput=null #If you don't want to make toms of logs you can set it null if you sent a file or some other options it will send all PHP output to this one. StandardError=/var/log/myphpdaemon.log [Install] WantedBy=default.target 

You will be able to start, get status, restart and stop the services using the command

You can use the PHP native server using php -S 127.0.0.1: or run it as a script. Using a PHP script you should have a kind of «forever loop» to continue running.

[Unit] Description=PHP APP Sync Service Requires=mysqld.service memcached.service After=mysqld.service memcached.service [Service] User=root Type=simple TimeoutSec=0 PIDFile=/var/run/php_app_sync.pid ExecStart=/bin/sh -c '/usr/bin/php -f /var/www/app/private/server/cron/app_sync.php 2>&1 > /var/log/app_sync.log' KillMode=mixed Restart=on-failure RestartSec=42s [Install] WantedBy=default.target 

If your PHP routine should be executed once in a cycle (like a diggest) you may use a shell or bash script to be invoked into systemd service file instead of PHP directly, for example:

#!/usr/bin/env bash script_path="/app/services/" while [ : ] do # clear php -f "$script_path"$".php" fixedparameter $ > /dev/null 2>/dev/null sleep 1 done 

If you chose these option you should change the KillMode to mixed to processes, bash(main) and PHP(child) be killed.

ExecStart=/app/phpservice/runner.sh phpfile parameter > /dev/null 2>/dev/null KillMode=process 

This method also is effective if you’re facing a memory leak.

Note: Every time that you change your «myphpdaemon.service» you must run `systemctl daemon-reload’, but do worry if you do not do, it will be alerted when is needed.

@LeandroTupone The MySQL and Memcached was a demonstration of how to use service dependencies it’s not necessary.

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:

  1. Call umask(0) to prevent permission issues.
  2. fork() and have the parent exit.
  3. Call setsid() .
  4. Setup signal processing of SIGHUP (usually this is ignored or used to signal the daemon to reload its configuration) and SIGTERM (to tell the process to exit gracefully).
  5. fork() again and have the parent exit.
  6. Change the current working dir with chdir() .
  7. fclose() stdin , stdout and stderr 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 :).
  8. Do your work!
Читайте также:  Linux mp4 to wmv

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.

Thanks for the info. It looks like libslack’s daemon program pretty much does all the prep work like you mentioned. I think for now I’ll stick with it until I find other good alternatives.

Found this post, expected code to copy&paste into a crappy old application that fails to close stdin etc., was disappointed. :p

For future readers asking why to fork twice: stackoverflow.com/questions/881388/… — TL;DR: Prevents zombies.

I run a large number of PHP daemons.

I agree with you that PHP is not the best (or even a good) language for doing this, but the daemons share code with the web-facing components so overall it is a good solution for us.

We use daemontools for this. It is smart, clean and reliable. In fact we use it for running all of our daemons.

EDIT: A quick list of features.

  • Automatically starts the daemon on reboot
  • Automatically restart dameon on failure
  • Logging is handled for you, including rollover and pruning
  • Management interface: ‘svc’ and ‘svstat’
  • UNIX friendly (not a plus for everyone perhaps)
  1. Use nohup as Henrik suggested.
  2. Use screen and run your PHP program as a regular process inside that. This gives you more control than using nohup .
  3. Use a daemoniser like http://supervisord.org/ (it’s written in Python but can daemonise any command line program and give you a remote control to manage it).
  4. Write your own daemonise wrapper like Emil suggested but it’s overkill IMO.

I’d recommend the simplest method (screen in my opinion) and then if you want more features or functionality, move to more complex methods.

There is more than one way to solve this problem.

I do not know the specifics but perhaps there is another way to trigger the PHP process. For instance if you need the code to run based on events in a SQL database you could setup a trigger to execute your script. This is really easy to do under PostgreSQL: http://www.postgresql.org/docs/current/static/external-pl.html .

Honestly I think your best bet is to create a Damon process using nohup. nohup allows the command to continue to execute even after the user has logged out:

There is however a very serious problem. As you said PHP’s memory manager is complete garbage, it was built with the assumption that a script is only executing for a few seconds and then exists. Your PHP script will start to use GIGABYTES of memory after only a few days. You MUST ALSO create a cron script that runs every 12 or maybe 24 hours that kills and re-spawns your php script like this:

killall -3 php nohup php myscript.php & 

But what if the script was in the middle of a job? Well kill -3 is an interrupt, its the same as doing a ctrl+c on the CLI. Your php script can catch this interrupt and exit gracefully using the PHP pcntl library: http://php.oregonstate.edu/manual/en/function.pcntl-signal.php

function clean_up() < GLOBAL $lock; mysql_close(); fclose($lock) exit(); >pcntl_signal(SIGINT, 'clean_up'); 

The idea behind the $lock is that the PHP script can open a file with a fopen(«file»,»w»);. Only one process can have a write lock on a file, so using this you can make sure that only one copy of your PHP script is running.

Читайте также:  Rsync in linux command

Kevin van Zonneveld wrote a very nice detailed article on this, in his example he makes use of the System_Daemon PEAR package (last release date on 2009-09-02).

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.

I recently had a need for a cross-platform solution (Windows, Mac, and Linux) to the problem of running PHP scripts as daemons. I solved the problem by writing my own C++ based solution and making binaries:

Full support for Linux (via sysvinit), but also Windows NT services and Mac OSX launchd.

If you just need Linux, then a couple of the other solutions presented here work well enough and, depending on the flavor. There is also Upstart and systemd these days, which have fallbacks to sysvinit scripts. But half of the point of using PHP is that it is cross-platform in nature, so code written in the language has a pretty good chance of working everywhere as-is. Deficiencies start showing up when certain external native OS-level aspects enter the picture such as system services, but you’ll get that problem with most scripting languages.

Attempting to catch signals as someone here suggested in PHP userland is not a good idea. Read the documentation on pcntl_signal() carefully and you will quickly learn that PHP handles signals using some rather unpleasant methods (specifically, ‘ticks’) that chew up a bunch of cycles for something rarely seen by processes (i.e. signals). Signal handling in PHP is also only barely available on POSIX platforms and support differs based on the version of PHP. It initially sounds like a decent solution but it falls pretty short of being truly useful.

PHP has also been getting better about memory leak issues as time progresses. You still have to be careful (the DOM XML parser tends to leak still) but I rarely see runaway processes these days and the PHP bug tracker is pretty quiet by comparison to the days of yore.

Источник

Оцените статью
Adblock
detector