php-cron – schedule jobs to run by the second and more
Yet another challenge, mostly like all other developers, ours at Saturn too believed that scheduled jobs could be run to the lowest frequency at every minute. This stands true if the job is handled only by *nix cron or by windows scheduler. For even further control one should write a Sleep-And-Run. I used to do this in shell script for most of my requirements. Just the other day some one challenged me to get done with one similar in php.
The outcome is a crude one, ServiceManager, which runs based on a configuration file and its run frequency. Best not to change the frequency or your configuration would never support anything less than the frequency as well as will only support multiples of the frequency. The configuration file is the famous ini file format with item names in square brackets, with configuration on two lines. The sample attached ServiceManager.ini should give an idea.
Overall this version of the ServiceManager will run for one hour before it exits, and the cron / scheduler could start this on every hour, making the system running continuously. Not taking pains to explain the code much, though some things cannot be skipped. Main thing is that the config file for the items and commands are read only on startup, so editing the ServiceManager.ini will not affect on a running system. To immediately exit a running system, create the abandond file. Please remember that if running from cron, the second instance would be launched at the next hour, and any manual instance would not have exited. The results would be un-predictable.
As usual for any snippets by me, there is a debug level check and some messages are echoed to the screen when in that debug level. Also to make running jobs at very fast instance, the normal cron behaviour of sending email from the out put is not accounted here and instead the output is logged into the ServiceManager::config['logfile']. php cron (555)
cron by second, php cron, php scheduler