3

I have a process I'd like to be able to run in the background by starting up a Gearman Client any time.

I've found success by opening up two SSH connections to my server, and in one starting the worker and in the other then running the client. This produces the desired output.

The problem is that, I'd like to have a worker constantly running in the background so I can just call up a client whenever I need to have the process done. But as soon as I close the terminal which has the worker PHP file running, a call to the client does not work - the worker seems to die.

Is there a way to have the worker run constantly in the background, so calling a new client will work without having to start up a new worker?

Thanks!

2 Answers 2

6

If you want a program to keep running even after its parent is dead (i.e. you've closed your terminal), you must invoke it with nohup :

nohup your-command &


Quoting the relevant Wikipedia page I linked to :

nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out.
The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.


For another (possibly more) interesting solution, see the following article : Dæmonize Your PHP.

It points to Supervisord, which makes sures a process is still running, relaunching it if necessary.

2
  • Thanks so much Martin, a great help as always! Will invoking nohup on a PHP script allow it to run forever, or would some sort of no-timeout need to be set as well? Apr 1, 2011 at 21:00
  • You'll still have max_execution_time (and, to a lesser extend memory_limit) to configure, so your script doesn't get killed after a while. Apr 1, 2011 at 21:02
3

Is there a way to have the worker run constantly in the background, so calling a new client will work without having to start up a new worker?

Supervisor!

The 2009 PHP Advent Calendar has a quick article on using Supervisor (and other tricks) to create constantly-running PHP scripts without having to deal with the daemonization process in PHP itself.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.