How to run cron jobs every 5, 10, or 30 seconds?
By default, cron checks crontabs for cronjobs every minute. If you want to run a job every n seconds you need to use a simple workaround.
Specify multiple jobs with offsets
The easiest way to run a job every n seconds is to run a job every minute and, and sleep in a loop in n second intervals.
Every 5 seconds
i=0
while [ $i -lt 12 ]; do # 12 five-second intervals in 1 minute
command/to/run & #run your command
sleep 5
i=$(( i + 1 ))
done
* * * * * script.sh
Every 10 seconds
i=0
while [ $i -lt 6 ]; do # 6 ten-second intervals in 1 minute
command/to/run & #run your command
sleep 10
i=$(( i + 1 ))
done
* * * * * script.sh
Every 15 seconds
i=0
while [ $i -lt 4 ]; do # 4 ten-second intervals in 1 minute
command/to/run & #run your command
sleep 15
i=$(( i + 1 ))
done
* * * * * script.sh
Every 30 seconds
i=0
while [ $i -lt 2 ]; do # 2 ten-second intervals in 1 minute
command/to/run & #run your command
sleep 30
i=$(( i + 1 ))
done
* * * * * script.sh
Every 30 seconds (different way)
* * * * * script.sh
* * * * * sleep 30 ; script.sh
-
How to get cron job errors in email with MAILTO?
One of the neat features of Cron is the ability to send emails when an error occurs during the execution of the cronjob. This can be done using the `MAILTO` environmental variable. When executing cronjob, any output is mailed to the owner of the crontab or to the user or email address specified in the `MAILTO` environment variable in the crontab, if such exists.
Questions -
How to prevent duplicate cron jobs from running?
Sometimes you may find that duplicate cronjobs are running at the same time. This may happen when the cronjob takes longer to complete than its execution interval. Here is a simple way to prevent this from happening ever again.
Questions -
How to set up a cron job for a specific time and date?
In this quick tutorial, we will take a look at how to set up a cron job to run at a specific time.
Questions -
How to view and read cron logs on Ubuntu, Debian and CentOS?
Cron can generate logs, which are very useful in troubleshooting your cron jobs. In this quick tutorial, we will take a look at cron logs – how to find them and how to read them.
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for us
Build on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
[email protected]or submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github