A Daemon is a Linux program that is constantly running in the background. In fact, Apache and MySQL are both Daemons. They sit around waiting for connections, i.e. Apache waits for remote web request connections to your sites and MySQL waits for connections from your PHP code trying to select and insert stuff into your database. Daemons are different from cron jobs in that they can run more than every minute, and basically every miniscule moment of the day. In PHP, they're a long-running script that you can program to wake up and go back to sleep while doing tasks whenever it wakes up and sees it has a pending task to do. It does so through loops. One cool thing to note is that most the Daemons on your Ubuntu Linux server store logs of what they’re doing in sub-folders and files within /var/log .
Most Daemons are written in C. However there are tools to make it very easy to create powerful Daemons in PHP.
I won’t go into the technical details of writing them because the following tutorial is fantastic:
Basically that tutorial describes tools the author made to create daemons, how to install the tool, and how to put it to use. Installing the tool is very easy. Just enter the following command at your command line:
# pear install -f System_Daemon
That requires the pear application to run, but fortunately you already installed that if you followed our Setup tutorials.
System_Daemon is a “pear package.” Pear packages are bundles of additional code you can attach to your PHP application that gives you additional functions/classes/methods to use. Pear itself is basically a tool designed for nothing bug augmenting the tools available to your PHP installation. You will use it often to install 3rd party PHP tools.
In this case after you’ve installed System_Daemon, you have access to a few a methods of the System_Daemon class that do all the real work of the Daemon, and the result is you can basically think and code in PHP terms you’re used to.
If you read that tutorial, you’ll see that the main function it offers is to loop through a task in an interval you set, i.e. it will attempt to perform the same task over and over again. You can then write code to do nothing if your code determines there is nothing to do. For example, if you want to check every moment for new users in your system and email them every time one is found, you can do so, and decide to do nothing if none are found. You can also safely break the system down if some error occurs.
You can also make it so your daemon script runs automatically every time you boot up your server. That way you don’t have to remember to turn it on and off again.
Another cool thing you can do is you can monitor the log file it’s writing too in realtime via the following command:
# tail /var/log/your-log-file-name.log
In the php code you write you specify the log file to write to in a simple configuration array and the daemon will write to it as it loops through the task its assigned. It will record each loop it makes, any errors, etc. So therefore you can monitor that file with the “tail” command and see what the daemon is doing in realtime. At FaceySpacey we really love the tail command. It puts what’s going on in your server at your fingertips. Often many things are going on in your server, and you’re not sure if it’s working or not, and you want to know immediately, possibly while you’re doing something to trigger breaking it. So for cron-triggered scripts and daemons this is very important since you’re not sitting there watching the results output to your screen. It’s all happening in the background. So “tail” is your window into what it’s doing.
Setting these Daemon tools up with Yii can be a little problematic. I’ll cover it in detail outside of these begginer set of tutorials, but the basic idea is that you put all the code you see in the Daemon tutorial (linked to above) into a Yii console application script. You can then trigger your console application script like you would normally from the command line, but then a Daemon is triggered and set into motion in the background. It’s really quite a powerful combination since you get access to all the Yii ActiveRecord tools to work with your database and models.