Skip to content

start-stop-daemon, Gearman and a little PHP

The scope of this blog entry is to give you a quick and dirty demo for start-stop-daemon together with a short use case on Gearman (all on Ubuntu). In this example, I'm using the start-stop-daemon to handle my Gearman workers through an init.d script.


Gearman is a queue! But unlike for example most of the backends to Zend_Queue, Gearman provides a little more than just a message queue to send — well — messages from sender to receiver. With Gearman it's trivial to register functions (tasks) on the server to make in order to start a job and to get stuff done.

For me the biggest advantages of Gearman are that it's easy to scale (add a server, start more workers) and that you can get work done in another language without building an API of some sort in between. Gearman is that API.

Back to start-stop-daemon

start-stop-daemon is a facility to start and stop programs on system start and shutdown. On recent Ubuntus most of the scripts located in /etc/init.d/ make use of it already. It provides a simple high-level API to system calls — such as stopping a process, starting it in a background, running it under a user and the glue, such as writing a pid file.

My gearman start script

Once adjusted, register it with the rc-system: update-rc.d script defaults. This will take care of the script being run during the boot process and before shutdown is completed.

A little more detail

The script may be called with /etc/init.d/script start|stop|restart (the pipes designated "or").

Upon start, we write a pidfile to /var/run and start the process. The same pidfile is used on stop — simple as that. The rest of it is hidden behind start-stop-daemon which takes care of the ugly rest for us.


As for Gearman, we use pecl/gearman and the code to run the workers is Queue_QueueController::gearmanworkerAction(), and it looks like the following:

The idea we had was to use pcntl to fork processes and handle signals from the main process (e.g. to stop a forked worker) process.

Missing pieces

The missing pieces are of course that you need to fill the queue. To do so, create a GearmanClient and submit data to the queue. Just check out the docs for gearman and in case anything is not so obvious, let me know in the comments. The tasks in the above example are "foo" and "bar" and located in my code in Queue_QueueController::bar() and Queue_QueueController::foo().

Gearman and PHP

When it comes to PHP, there are various options to interfact with Gearman. Gearman itself is currently available in two flavours — Perl and C. The C-server is a complete rewrite of Danga's original implementation in Perl.

In order to use the C-server, you will have to use pecl/gearman. If for some reason you don't want that, use Net_Gearman (active fork by Brian Moon) and install the Perl server. On that note — the C-server also requires some launchpad-fu, while the original Perl implementation (on 9.10) is available through apt-get install gearman-job-server.

That was, last time I checked. Regardless of what's available right away, the launchpad packages are always more updated and current.


Through PECL: pecl install gearman

Through PEAR: pear install Net_Gearman-alpha

(To get those commands, just apt-get install php-pear first.)


That's all kids! :) If you want more in-depth about Gearman, let me know in the comments.


No Trackbacks


No comments

The author does not allow comments to this entry