Dear All, I have an application where from time to time I need to check the db for some value and run some functions to do some checks. What is the best option do it as php daemon or cron job?What I worried of cron job is the overalapping.
My first question would be, how you implement that kind of deamon in code. If you do not have the need to directly call the deamon to run and it's ok for you to have the job run on a static schedule, a cronjob should be fine for you. The mentioned issue of overlapping can be cought when using a kind of lock-file or similar to check whether your script is already / still running. Based on that you can decide whether you end this probably overlapping job run or if you just kill the old process....
Better use a cronjob. A daemon written in php tends to accumulate more and more memory until it fails after a few days. PHP is not a good language to write deamons. Regarding the overlapping cron job, create a lockfile when your php code starts and there is no lockfile. This file gets deleted when your code is finished. If your script is run and finds already a lockfile, it quits directly.
Dear Till, I dont quite get the idea of lock file. How do I go about I am very new to it? Can you give me more description?
Here some php code as explanation (untested): <?php // Clear the statecache clearstatecache(); // we will stop here when a lockfile exists if(is_file('/tmp/mylockfile')) die('There is already a instance of this script running'); // Set the lockfile touch('/tmp/mylockfile'); // Here we do some work that may take a few minutes sleep(600); // We are finsihed, so we remove the lockfile unlink('/tmp/mylockfile'); ?> When you execute this script a second time while the first one is still running, it will not execute the code (here the sleeep command) again. Instead it exits with the message "There is already a instance of this script running".
The baove code is an example for a file based locking mechanism. You can run it on the shell or as cronjob, thats up to you. But running that script as cron makes not much sense as it does nothing else as waiting for 600 seconds. So if you want to test it, then do the tests on the shell and add you own code before you create a cronjob.
This may be a bit oldschool but... Code: while(1) { //some logic //sleep for some period of time } Then make the script executable and run it. You can run it in the background or even set it up to run at boot time.
Dear Till, To elaborate further actually I capture my data via a java listener then I insert into the db. Then I will pick those unprocess and process them via this php or cron job. Do you think I should process it in the java itself? Would it make the java programme to be burdened?
May anser was only regarding the message from @eyeoncomputers and why it is better to use a cronjob and not run a daemon in php. If you process your data in java or php is up to your programming skills, use the language that fits better for you.
Dear Till, I mean based on your experience will be better to handle those queries in java itself or do it separately after the insert into db via another php. What is your opinion?