One of the cool facts about PHP is that you can use it for many different tasks. For
instance, many developers (included myself) use PHP to perform automation tasks, such as:
Automatic email alerts Sending emails at specific intervals or when some events occur.
Database maintenance and clean-up
Cleaning old table records, periodic table checking and optimization, etc.
Data analysis Creating statistical data and keeping it up to date, performing analysis on large data sets, creating periodic reports, etc.
And much more. The classic way of executing these scripts is by using Cron. Each script is executed by Cron at a specific date and time, or at specific time intervals.
But there is another, less known alternative: creating a PHP daemon. So, let's see what PHP daemons are and how they can make your programming life easier.
On Linux systems, background processes are called "daemons". Similarly, PHP daemons are PHP scripts that keep running in the background. PHP daemons execute their tasks periodically, at specific time intervals or when some events occur.
How does a PHP daemon work? A standard PHP script executes its task and then exits right away. A PHP daemon, on the other hand, does not exit after executing its task. Instead, it keeps sleeping for some time, and then executes its task again, over and over.
This is the basic code template of a PHP daemon:
$sleepTime = 3600; while (true) { task(); sleep($sleepTime); }
This script executes the task() function every hour (3600 seconds). It is said that the script iterates every 3600 seconds, and
that 3600 seconds is its iteration time. Of course, you can change task()
with any piece of code.
Here is a slightly more complex template:
$sleepTime = 3600; while (true) { if ($condition) { task(); } sleep($sleepTime); } Now the daemon checks for a condition before executing the task() function. This condition can be anything: a specific date and time (this way you can simulate a Cron-based execution), a value read from the database, and so on.
If you need to perform different automated tasks, PHP daemons can make your life easier because they are incredibly flexible. Compared to Cron-triggered scripts, they are more customizable and you can control them with more precision.
You can adjust the iteration time to fit your needs. A shorter iteration time makes the script more responsive, but it also makes it consume more system resources. In some advanced configurations, the iteration time can also be changed dynamically by the script itself during its iteration.
Cron-triggered scripts VS daemons. When you use Cron to fire your scripts, you can only execute them at specific
times or at specific time intervals. A daemon, on the other hand, can iterate at specific times or time intervals (just like Cron can) and, on top of that, can also iterate when some kind of condition is met.
There are a lot of possible conditions you can use, including:
A specific date and time
A value read from the database
An input submitted by remote users
The time elapsed since the last iteration
PHP daemons also solve some Cron-related issues. For example, if you use Cron and the script is executed often (like every 2 minutes), there is a chance that when the next execution starts the previous
one is still running. I did experience this with Cron, and I had to implement a locking system.
With PHP daemons this problem doesn't exist, because the next iteration always starts after the current one is finished.
PHP daemons are also more versatile because you can control them from a web-based interface. It work like this:
From the web interface you set some values on a dedicated database table.
The daemon reads these values before or during its iteration, and changes its behaviour accordingly.
For example, you can set a "pause" flag to make the daemon skip its iteration. And you can even change the iteration interval simply by giving the daemon the new interval value.
PHP Security Mastery What if you could write code that is always secure from attacks? Just imagine how this would increase your reputation and your possibilities as a developer. That is exactly what you will get from this course. You will learn to use the defense techniques that really work, leaving nothing to chance (with real code examples).