How to create a PHP linux daemon (service)

There are many ways to automate scripts and applications running on Linux, you can obviously create a cron task, that can execute from between a minute to once a year if you wanted there are however other ways in which you can have unattended background tasks execute.

Today I will explain how you can create a Linux daemon using ‘Upstart’, this tutorial has been carried out on Ubuntu Server 14.04 and will work with various other distributions too. If you wish to find out more about Upstart check out it’s project website.

To demonstrate it works we will create a small PHP script that will literally append a text file with a timestamp every couple of seconds!

A few benefits of running a PHP script as a daemon as opposed to a cron task are as follows:-

  • You can easily start and stop the execution of these scripts without having to edit the crontab each time (simply issue a single command like ‘stop my-service-name’).
  • You can run tasks more frequently than once per minute (which is currently the crontab’s minimum execution winow)
  • Specify which run-levels your script is to be run on.
  • …plus various others!

It is actually amazingly easy to create a Linux daemon using Upstart, you simply create a daemon configuration script under /etc/init (take not, NOT /etc/init.d as per the older  System-V init scripts)

This is what our service script will look like, its pretty simple, there are many more awesome directives you can use for your service configuration script such as ‘pre-start’ configuration so your service can go and create initial directories or file etc. that it may depend on (check out the Upstart documentation for a full list of configuration directives.)

description "Bobbys Test Daemon"
author "Bobby Allen"

start on startup
stop on shutdown
respawn

script
 sudo -u root php -f /usr/share/test_daemon.php
end script

As you can see, we reference our PHP script within the configuration file, this is what will be run by the service, keep in mind that a daemon/service is designed to be ran continuously in the background and will only stop when told to.

You must keep in mind that you must develop your PHP service script to ‘loop’ continuously as by default a PHP script will run once and then exit out, here is a very quick example script of which should be placed on the filesystem somewhere and then referenced accordingly, in my example I’ve just placed it under /usr/share/test_daemon.php

The contents of the PHP script is as follows:-

 <?php

// The worker will execute every X seconds:
$seconds = 2;

// We work out the micro seconds ready to be used by the 'usleep' function.
$micro = $seconds * 1000000;

while(true){
 // This is the code you want to loop during the service...
 $myFile = "/home/ballen/daemontest.txt";
 $fh = fopen($myFile, 'a') or die("Can't open file");
 $stringData = "File updated at: " . time(). "\n";
 fwrite($fh, $stringData);
 fclose($fh);

 // Now before we 'cycle' again, we'll sleep for a bit...
 usleep($micro);
}

You should now be able to start, stop and view the status of your new linux daemon like so:-

status test-daemon
start test-daemon
stop test-daemon

If you start the daemon using the command ‘start test-daemon‘ and give it 10 seconds, then stop it using ‘stop test-daemon‘ you should notice when you read the log file that our daemon is writing to every 2 seconds then it should include 5 lots of timestamps proving that the daemon is working successfully! (obviously it will have more if you’ve already been playing with the start command :))

 

Share