How To write a background daemon server process

Sometimes we want a PHP script to run all day and all night as a background process, not directly under control of the user, but as a system like daemon. This is surprisingly easy thing to do with a PHP Script.

Here is a minimal example that I saved as myDaemon.php


#!/usr/bin/php

<?php

$pid = pcntl_fork () ;
if( $pid ) {
// parent process runs what is here
echo “\nParent :: pid=”.getmypid ();
echo(“\nParent Process exiting\n”) ;
exec( “kill ” . getmypid () ) ;
}
else {
// independent child process runs what is here
echo “\nMy independent Server started as PID : “.getmypid ().”\n” ;
echo “kill “.getmypid ().”\n\n”;
Main () ;
}

function Main() {
$life_span = 30;
$stop = time() + $life_span ;
exec( “kdialog –progressbar \”Connecting to Server\” 30 “,$out,$ret);
$qdbus_ref = $out[0];
echo “\$qdbus_ref = $qdbus_ref\n”;
while( time()<=$stop ) {
$percent = $life_span-($stop-time());
echo “\$percent = $percent \n”;
exec( “qdbus $qdbus_ref Set org.kde.kdialog.ProgressDialog value $percent” );
sleep(1);
}
}

 

Now, if we open a command line konsole and execute this small script

php -f myDaemon.php

You should see the process run. The mysterious Main() runs for 30 seconds, and for those 30 seconds it shows a KDE Progress bar.

Example output :

Parent :: pid=21234
Parent Process exiting
Terminated
eg@kuznetsov:/media/new-home/eg/Project/Daemon$
My independent Server started as PID : 21235
kill 21235

$qdbus_ref = org.kde.kdialog-21238 /ProgressDialog
$percent = 0
$percent = 1
$percent = 2
$percent = 3
$percent = 4
$percent = 5
$percent = 6
$percent = 7
$percent = 8

 

While it runs, you can NOT stop the process. If you press Ctrl-C it will not stop the process, if you close the console you will not stop the process. The child process has grown up an left home. The parent can no longer control the child process.

The only way to stop it is to type kill and the pid, the process number. If you get really stuck, press Ctl-Esc and search for myDaemon or PHP to find the critter and kill it.

This is why function Main() is all about running for just 30 seconds and giving you some feedback.

Of course, in real life you will want better strategy. For developing a script I suggest that the first thing you do is write a tiny function that writes a shell script to kill the process.

#!/bin/bash
kill 12345

You should also think about using a lock file with the pid process number. This can be used to prevent more than one instance of the script running. The other thing to give some thought is how to start and stop the process. KDE has organised the simple addition of startup scripts, so adding it to KDE is easy, and for small background scripts that is a reasonable practical lightweight solution.

I also describe how to easily find out if the KDE Desktop is busy or idle, by a quick and easy call to D-Bus, this could be added into myDaemon.php so that the script only does heavy work when the desktop is idle.

Advertisements

One thought on “How To write a background daemon server process

  1. Pingback: Write your own independent background process using PHP | evadinggrid

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s