Running cron every second ….

February 8, 2015 – 01:00

Can we do this ? well maybe this question appear in the first place when you think about it. Off course it’s in condition when you are familiar with cron in *nix OS platform.

Just got me to the point right now ….

OK ok if you insist … here it’s the trick how to do it ….First, set your mind that more detail you want to run your cron, more entries at crontab configuration file.

Second, make sure the job you are executing is not exceeded time delay amount.

The magic trick here is using sleep utilities of current operating system you are using right now. Because in the default, cron job only execute every minute minimum, so you need run it at the current minute, but delay it a little bit, so it look like execute in the time range you delayed.

For example, if you want to run your cron for every 5 seconds, here it’s the entry at the crontab file :

* * * * * commandExample.sh
* * * * * sleep 5;commandExample.sh
* * * * * sleep 10;commandExample.sh
* * * * * sleep 15;commandExample.sh
* * * * * sleep 20;commandExample.sh
* * * * * sleep 25;commandExample.sh
* * * * * sleep 30;commandExample.sh
* * * * * sleep 35;commandExample.sh
* * * * * sleep 40;commandExample.sh
* * * * * sleep 45;commandExample.sh
* * * * * sleep 50;commandExample.sh
* * * * * sleep 55;commandExample.sh

Here it’s the explanation :
When it’s time to execute the cron, all of above lines are executed immediately. But actual command (i.e. commandExample.sh) is delayed accordingly. So it’s looks like the actual command is executed every 5 seconds.

Please again, note, like second note above, if your command need time more that specified, there will be queue that will add more and more as time goes.

That’s all, hope you like it and useful for you :)

Post a Comment

CommentLuv badge