How can I run sub-minute cron jobs? - cron

I would like to run a cron job every Wednesday at 10:31:10, but I just learned that crontab cannot run sub-minute jobs, so the closest I can get is 10:31 a.m. with the below code:
31 10 * * WED /file/to/run.py
Is it possible to hack around this, or are there other alternatives to cron that could do the job?

The easiest solution is to sleep for 10 seconds:
# .----------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
31 10 * * 3 sleep 10 && /file/to/run.py

You can't. Cron has a 60 sec granularity.
You can though build a SH script that sleeps for ten seconds and then does X, set your cron job to run a script at 10:31 AM on a wednesday that then sleeps for 10 seconds, then do x.

Related

Crontab to run at two hours out of the day, but for one of those hours exclude certain days of the week

I need to change the below schedule to exclude Tuesday and Thursday for only the 2nd hour.
0 2,20 * * *
I want to run a job everyday at 2 am and 8pm, but on Tuesdays and Thursdays exclude the 2 am job.
The easiest answer is going to be two separate entries in your crontab:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* 2,20 * * 0,1,3,5,6 root /path/to/command.sh
* 20 * * 2,4 root /path/to/command.sh
Make sure that command.sh isn't in an /etc/cron.* folder.
I think all you need is
0 2,20 * * 1,3,4,6,7
the numbers at the end are the days of the week. O being Sunday.

How can I create Cron Expression which works between particular times

I am working on a Cron Expression for a job that has to run on weekdays every 10 mins from 7.30 am to 8.20 am(inclusive).
The trigger used by me:-
0 30/10 7-9 ? * SUN,MON,TUE,WED,THU *
Issue is it works beyond 8.20 as well? Can we limit it to specific minutes or end time? like 7:30-8:20
Some things you cannot do in a single expression and you might consider to use two:
# Example of job definition:
# .--------------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
# This runs on 7:30, 7:40, 7:50
30/10 7 * * 1-5 command
# This runs on 8:00, 8:10, 8:20
0-20/10 8 * * 1-5 command
Another way you could attempt this is by using a test.
# Example of job definition:
# .--------------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
*/10 7,8 * * 1-5 testcmd && command
Where testcmd is an executable script that could look like:
#!/usr/bin/env bash
hours=$(date "+%H")
minutes=$(date "+%M")
(( hours == 7 && minutes >= 30)) || (( hours == 8 && minutes <= 20 ))
Other examples of this trick can be found here:
Cron expression to run every N minutes
How to schedule a Cron job to run 4th week of the year
how to set cronjob for 2 days?

Cronjob understanding: how to run every day from 8-10 till 10-30 every 10 and 30 minute?

I need to run task every day from 8-10 till 10-30 every 10 and 30 minute:
start at 8:10 ->
8:30 ->
9:10 ->
..
-> 10:30 finish
I have such cronjob:
10,30 8,9,10 * * *
will it be correct?
Yes, it is fine!
You can check it in http://crontab.guru/#10,30_8,9,10_*_*_*
Since you want to run it in an interval of hours, you can also say 8-10 to match hours from 8 to 10:
10,30 8-10 * * *
For your future reference, this is the format for cronjobs:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
A briefly shorter version would be
10,30 8-10 * * *
but yes, your version also works fine.
If you want to play around a bit, you can try crontab.guru.

Cron Scheduling format guidance

I want to run a cron every 30 seconds between 00:00 and 11:55, every day, every month, any day of the month.
Is the following correct?
0/30 * 0-11 **
The format is as follows:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
So if you want to run every minute between 00.00 and 11.55, every day, every month, any day of the month, you need to combine two different lines:
* 0-10 * * * command
0-55 11 * * * command
Note that to run every 30 seconds you can use the trick described in Running a cron every 30 seconds.
You can try to run your script every 30 seconds using the following:
* 0-11 * * * (sleep 30; /path/to/executable)
So your crontab should looks like
* 0-11 * * * /path/to/executable
0-54 0-11 * * * (sleep 30; /path/to/executable)
Both command lines will be executed at the same time, but the second one will do a 30 seconds sleep before executing your command.
You can try to validate your cron statement with decoder
One of them you can find by the link: http://cronwtf.github.io/

Run cron job every minute during specific hour during 30 minutes

I know I can set up cron for every minute, like
* * * * *
for once a day AFAIK it would be (lets say on 2am)
0 2 * * *
for every 30 minutes
0,30 * * * *
Now, is it possible to run cron job every minute, but during 30 minutes, once a day. For example I need the cron to run every day from 2am to 2:30, and during that time every minute.
thanks
Yes, sure. Use this:
0-30 2 * * *
^^^^ ^
| |
| on hour 2
on minutes from 0 to 30
Remember the format is as follows:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed

Resources