Contents...
In this tutorial I will help you to run cron in every 5 seconds. Crontab stands for “cron table” so we can say that crontab is a list of table which you want to run it regularly. As a system admin it is one of the challenges task to execute your work when you are available. Basically cron is a process that automatically performed as per your scheduled time.
Format of Crontab
MIN HOUR DOM MON DOW CMD
Crontab Fileds
Field Description Allowed Value MIN Minute field 0 to 59 HOUR Hour field 0 to 23 DOM Day of Month 1-31 MON Month field 1-12 DOW Day Of Week 0-6 CMD Command Any command to be executed.
Cron is design to run a task in minutes and was not designed to run every x seconds to run something. So you have to run your task within a loop. Crontab job can be used to schedule a job in minutes/hours/days, but not in seconds you need an alternative way.
Learn more: Top 20 Crontab Example to Schedule Task
In this article I will provide you alternative way to run cron every 5 seconds.
Run Cron in Every 5 Seconds
In a cron we can not schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. Tc Here I am going to create a scripts which will run in loop, we can say that it will run in every 5 seconds.
# cd /home/sagar/ # cat 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
In the above example I have a MySQL slow query kill scripts. This script will check the long running query in every 5 second on the database and kill the query as mentioned condition in the scripts.
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
FAQs
How do I run a cron job every second?
Running a cron every second is not possible but you can run it every second with alternate way. You can use sleep command, to do this you have to create a scripts a define sleep command for example every X seconds.
Where can I find crontab logs?
You can find crontab log in /var/log/syslog. It is by default path to store all cron related log here.
How can I check crontab is working or not?
To check the crontab weather it is running or not just run “systemctl” command with status option. There is a status like : “Active (Running)” so it confirmed that crontab is working properly.
How can I run script every 5 minutes?
To run script every 5 minutes, edit your crontab file using “crontab -e” command. Now add the following line for every 5 minutes interval. For example: */5 * * * * * /path/to/scripts.sh
How can I edit crontab?
To edit your crontab just run “crontab -e“command. Edit the file and check with “crontab -l” command.
If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.
Version for cron
# cd /home/sagar/
# vim five_seconds.sh
#!/bin/bash
for i in `seq 1 12`
do
/home/sagar/kill_long_query.sh
sleep 5
done