I definitely do not recommend using a cronjob for this.
cronjobs is good and very useful and easy for many purposes, but as you describe your needs, I think that they can create more complications than they do well. Here are a few things to consider:
What happens if jobs overlap? execution takes longer than one minute? Are there shared resources / locks / temporary files? - The most common method is to use a lock file and terminate if it is busy at the very beginning of the program. but the program should also look for additional tasks immediately before its completion. - this, however, can also get complicated on Windows machines because they AFAIK do not support write locks out of the box.
cronjobs is a pain in the ass to maintain. if you want to control them, you need to implement additional logic, for example, checking when the last program was launched. this can be difficult if your program should run only on demand. the best way would be some kind of "filling the job" in the database or deleting the rows that were processed.
on most unix based systems, cronjobs are pretty stable, but there are many situations where you can crash your cronjob system. most of them are based on human error. for example, sysadmin, which does not exit the crontab editor in the correct mode, can delete all cronjob. many companies also do not have an adequate monitoring system for the above reasons and notify as soon as their services experience problems. at the moment, often no one has recorded / entered into version control, which cronjobs should run, and wild guessing and recovery begins.
Cronjob support can be even more complicated when external tools are used and the environment is not its own unix system. sysadmins need to gain knowledge of more programs, and they may have potential errors.
I honestly think this is a small script that you run from the console, and let open fine.
<?php while(true) { $job = fetch_from_db(); if(!$job) { sleep(10) } else { $job->process(); } }
you can also touch the file (change the modification timestamp) in each loop, and you can write a nagios script that checks that this timestamp is out of date, so you know that your work is still working ...
if you want it to start with the system, I recommend deamon.
ps: the company I work for has a lot of background activity for our site (crawl, update processes, calculations, etc.), and cronjobs were a real mess when I started there. they were distributed across different servers responsible for different tasks. Databases were accessed wildly over the Internet. a ton of nfs filesytems, samba, etc. were available for sharing resources. the place was full of single points of failure, bottlenecks and something constantly broke. there was so much technology that it was very difficult to maintain, and when something didn’t work, it took him hours to track the problem and another hour of what that part was supposed to do.
Now we have one unified update program that is responsible for literally everything, it works on several servers, and they have a configuration file that determines the tasks that are performed. eveyrthing is dispatched from one parent process, executing an infinite loop. It is easy to control, configure, synchronize and everything works smoothly. it is redundant, it is synchronized and the granularity is beautiful. therefore, it runs in parallel, and we can scale to as many servers as we like.
I really suggest sitting down long enough to think about everything in general and get a picture of the complete system. then take the time and effort to implement a solution that will function well in the future and will not distribute many different programs on your system.
imp:
I read a lot about the minimum interval of 1/5 minutes for cronjobs / tasks. you can easily get around this with an arbitrary script that takes this interval:
// run every 5 minutes = 300 secs // desired interval: 30 secs $runs = 300/30; // be aware that the parent interval needs to be a multiple of the desired interval for($i=0;$i<$runs;$i++) { $start = time(); system('myscript.php'); sleep(300/10-time()+$start); // compensate the time that the script needed to run. be aware that you have to implement some logic to deal with cases where the script takes longer to run than your interavl - technique and problem described above }