Cronjob more than once a minute: e.g. every 10 seconds!
07/16/2019 (2623x read)
Normally one minute is the smallest unit to run a cronjob. But if you need the automatic execution of programs or scripts more often, you can use a trick: This way you can execute a script every 30, 15 or 10 seconds and not only once per minute!
Cronjob once per minute:
* * * * /home/user/cronjob.sh
Cronjob once per hour
1 * * * * /home/user/cronjob.sh
Run a Cronjob every 30, 15 or 10 seconds!
While the cronjob can be used by default only once per minute, you can use this trick to execute the cronjob more often per minute. Strictly speaking, you are using the same cronjob more than once per minute, but with a waiting time: So you can execute a cronjob more than once per minute!
Cronjob every 30 seconds
To execute a script every 30 seconds, you simply create the cronjob twice: Once will start normally every minute, the other with „sleep 30“-command in front of it. While the first script is started directly once per minute, the second script waits 30 seconds first. So your script runs directly twice per minute – or every 30 seconds.
* * * * /home/user/cronjob.sh * * * * * sleep 30; /home/user/cronjob.sh
Cronjob every 15 seconds
This also works with other times: To run the script every 15 seconds, you have to start it four times and adjust the sleep time:
* * * * /home/user/cronjob.sh * * * * * sleep 15; /home/user/cronjob.sh * * * * * sleep 30; /home/user/cronjob.sh * * * * * sleep 45; /home/user/cronjob.sh
Cronjob every 10 seconds
However, it is also possible that a cron job is executed every 10 seconds: Therefore, you’ll need to run it six times and adjust the sleep time by 10 seconds each time:
* * * * /home/user/cronjob.sh * * * * * sleep 10; /home/user/cronjob.sh * * * * * sleep 20; /home/user/cronjob.sh * * * * * sleep 30; /home/user/cronjob.sh * * * * * sleep 40; /home/user/cronjob.sh * * * * * sleep 50; /home/user/cronjob.sh