Cron’s granularity is in minutes and was not designed to wake up every x seconds to run something. Run your repeating task within a loop. Crontab job can be used to schedule a job in minutes/hours/days, but not in seconds you need a alternative way.
In this article I will provide you alternative way to run cron every 5 seconds.
Run Cron in Every 5 Seconds
Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses sleep 5 command in it.
# cd /home/sagar/ # vim five_seconds.sh #!/bin/bash while true do /home/sagar/kill_long_query.sh sleep 5 done
Make this script executable typing below command:
# chmod +x /home/sagar/five_seconds.sh
Now, execute this shell script in the background using nohup as shown below. This will keep executing the script even after you logout from your session. This will execute your backup.sh shell script every 5 seconds.
# nohup ./five_seconds.sh &
You can also set cron job to execute this script like below:
* * * * * /home/sagar/five_seconds.sh > /dev/null