Php daemon vs Cron Job

Dear All,
I have an application where I have to time to time keep checking the db if there is any update I got to run some function. So what is recommended cron job or php daemon.

I would recommend a cron job. Writing a daemon in PHP is more complicated, while cron will do exactly what you need.

Dear Immerse,
I read for cron there is one problem of overallping. Say my cron start at minute one and stretch over minute 2 then at minute 2 cron starts another instance of it and if this goes one the server will go down any solution for this?

Increase the interval i would say.

Writing a daemon is ok in theory but then you’d need to be sure you can run it and on a shared server this is often not allowed.

For the overlap you can also use a lock file. Take a file name, e.g. “cron.lock” and check if it exists when the cron starts. If it does don’t continue. If it doesn’t create one and delete it when you’re done.
That, plus you know that cron jobs won’t start at the exact moment (which could still give issues with the lock file) guarantees you’ll never have more than one running at the same time.
The only annoyance here is that if a cron job created the file and then crashed the file will still exist and the cron job will never run anymore until you delete that file.

Dear Scallio,
How do we create the file via shell script is it? Any codes for that ? Secondly in the event of crash how to go about it?

Dear Tangoforce,
Server is not an issue we have our own dedicated ones. If you say increase the interval referring to cron or php daemon? U have any link how to write basic phpdaemon?

You could do something like this:


$lockfile='./cron.lock';
$timeout=30 * 60; // 30 minutes
if (file_exists($lockfile))
{
  $fp=fopen($lockfile, 'r');
  $ts=fgets($fp, 4096);
  fclose(fp);
  if ($ts > time() + $timeout)
    exit();
}
$fp=fopen($lockfile, 'w+');
fputs($fp, time());
fclose($fp);

// rest of cron code

@unlink($lockfile);

When the cron starts it will write the current timestamp to the file. If another cron job starts it will open that file and read the timestamp. If it’s less than 30 minutes ago the cron will not run. If it was written 30 minutes or more ago it will run (under the assumption that some other process crashed earlier without deleting the file).

Dear Scallio,
Thank you for the code snippet. Give me some time I need to see how to run this file and adapt it. So I save this file as .php is it? Then I call it via shell script to be a cron is it?

Yes and yes :slight_smile:

Of course you need to put your own code in the spot where I put // rest of cron code

Dear Scallio,
I know where to put my codes ready thank you. I will try if not I will get back to you. Another thing say I save this file as service1. php so under crontab -e can I put like this */1 * * * * /usr/local/service/service1.php. Then restart the cron service?

No, you can’t execute php files by themselves. You need to call the php exectutable with the php script as a parameter


*/1 * * * * /path/to/php /usr/local/service/service1.php

you need to put the full path there, just “php” might not work because cron jobs aren’t run in the same environment you are in as user.

If you don’t know where the php executable is, type


whereis php

Dear Scallio,
Below is the results of whereis php. So my guess will be /usr/bin/php is it?
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/share/php /usr/share/man/man1/php.1.gz

Yup :slight_smile:

Dear Scallio,
Give me some time now to play around as I am very new to all this. Thank you once again.

I meant to increase the delay in the cron however Scallio has also put forward a pretty good idea.

With regards to a daemon, see my comments below.

Indirectly you can using a loop, set_time_limit(0) and ignore_user_abort(true). Call the page, click the stop button and the script will continue looping until it say finds a ‘stop’ file or a marker in a database. Not quite the same but it would be easier than creating a daemon to run as its own process. I know i’ve used this technique for several things and it works well on both windows and *nix systems. I’ll admit its a bit crude and possibly frowned upon by the programming elite but it does work and its simple to put into action.

Sample here (untested):


//No time limit
set_time_limit(0);

//Ignore the browser closing connection
ignore_user_abort(true);

while(!file_exists('stopfile.txt'))
   {
   //Do something

   //Anti CPU locking
   sleep(1);
   }

I know its crude but it does the job…

Dear Tangoforce,
When you say a daemon here is to create php file and let it run under linux cron am I right just to make sure I am using the right terminology. I dont quite get your method when you use the stopFile.txt who creates the file and what to store in that file? Thank you.

A daemon is a process / program that runs by itself. Like a windows .exe program. I got this from dictionary.com:

daemon definition
operating system
/day’mn/ or /dee’mn/ (From the mythological meaning, later rationalised as the acronym “Disk And Execution MONitor”) A program that is not invoked explicitly, but lies dormant waiting for some condition(s) to occur. The idea is that the perpetrator of the condition need not be aware that a daemon is lurking (though often a program will commit an action only because it knows that it will implicitly invoke a daemon).

You started this topic by talking of creating a daemon. I think most of us assumed you might write a program in c/++ or something similar that would run on your server.

All I am saying is that you can do that, run a cron script OR run a php script in a loop as i have demonstrated which would run in a similar method to both of the above. The idea is that the script would loop continuously (sleeping for 1 second to avoid locking the cpu) until you tell it to stop - eg by placing a small text file on the server. You don’t even need anything in the file just a blank file with the correct filename would do it.

I’m just trying to give you one more option to consider.

Dear Tangoforce,
Thank you for the indepth explain have given me more clarifications. What I am a bit confuse here is that when you php script in a loop ? How do you execute that script? Can brief me on that ? Thank you.

You call it via the browser like any other php script. You won’t see any html output but you can then just hit the stop button it will continue looping.

You could theoretically do this to confirm its running but its not guranteed:


print 'Running';
flush();