How can I create Cron Expression which works between particular times - cron

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?

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.

Crontab days exceptions Linux

I have a crontab job that works from monday to friday, but i need it to make exceptions of an specific day, for example the January 1st, the April 11th, etc.
How can i make that exception on my crontab job?
* * * * 1-5 ./full-backup
The easiest way is to use an and or or list sequence.
man sh: AND and OR lists are sequences of one of more pipelines separated by the && and || control operators, respectively. AND and OR lists are executed with left associativity.
An AND list has the form command1 && command2.
command2 is executed if, and only if, command1 returns an exit status of zero.
An OR list has the form command1 || command2. command2 is executed if and only if command1 returns a non-zero exit status.
If you want to exclude 2 days from your cron, lets say the first of January and April 11th, then you can do the following:
# .---------------- 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
* * * * 1-5 [ `date '+\%m\%d'` == "0101" ] || [ `date '+\%m\%d'` == "0411" ] || ./full_backup.sh
From the moment you have more days to exclude it becomes a bit more tricky. You could use a smaller script like excludedaycmd
#!/usr/bin/env bash
while [[ $1 != "" ]]; do
[[ $(date "+%m%d" )) == $1 ]] && exit 1
shift
done
exit 0
This script will exit with 1 if any of its arguments fit a day. Your cron would look then like.
# .---------------- 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
* * * * 1-5 excludedaycmd 0101 0411 && ./full_backup.sh
Any other script can also be used in any other form.

How can I run sub-minute cron jobs?

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.

How can i run cron-job every 1 hour in Ubuntu 14.04?

I want to run a cron for every 1 hour.
What i tried :
0 */1 * * * /home/username/test.sh
0 * * * * /home/username/test.sh
But, i am not sure, which one is right ?
Can you please help me to decide . which one i should use?
This is the way to set cronjob on ubuntu for every 1 hour
0 * * * * or you can put it in cron.hourly directory
For more details you can refer link:
https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job
# Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
#--------------------------------------------------------------------------
* */1 * * * root /bin/sh /home/username/test.sh

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