execute crontab twice daily at 00h and 13:30 - cron

i want to execute a script twice daily at 00:00 and 13:30 so i write :
0,30 0,13 * * *
it seems wrong for me, because like this, the script will fire at 00:00 , 00:30 , 13:00 and 13:30. Any idea ?

Try this-: 00 01,13 * * *
it will run at 1 A.M and 1 P.M

You can't do what you want in one entry, since the two minute definitions will apply for both hour definitions (as you've identified).
The solution is (unfortunately) use two cron entries. One for 00:00 and one for 13:30.
An alternative is perhaps to execute one script at 00:00. That script would execute your original script, then wait 13.5 hours and then execute that script again. It would be easy to do via a simple sleep command, but I think it's unintuitive, and I'm not sure how cron manages such long running processes (what happens if you edit the crontab - does it kill a spawned job etc.)

You CAN NOT do that with cron on a single line.
You have to create 2 separate lines like so:
# Will run "YourCommand" at 00:00 every day of every months
#Min Hours D of the M Month D of the Week Command
0 0 * * * YourCommand
# Will run "YourCommand" at 13:30 every day of every months
30 13 * * * YourCommand
Or, as a single line, you can run a command every x hours, like so:
# Will run "YourCommand" every 12 hours
0 */12 * * * YourCommand
or
# Will run "YourCommand" at 1am and 1pm every day
0 1,13 * * * YourCommand arg1 arg2

Try this out: 0 6,18 * * *
it will run at minute 0 past hour 6 and 18
Or you can try it out on cronguru

try ...
00,30 00,13 * * * [ `date +%H%M` == 1330 ] || [ `date +%H%M` == 0000 ] && logger "its time"

Try this out:
0 1,13 * * *
What the above code means:
Cron will run at minute 0 past hour 1 and 13
Sharing a screenshot from crontab.guru

30 0,13 * * * somecommand.sh
This is just purely an example, but you will see that this is a cron entry that will run at 0:30AM and then 1:30PM (13 is 1 in military time). Just comma separate the hours, or comma separate whatever section of the cron.

try this,
0 10 9/12 ? * *
At second :00, at minute :10, every 12 hours starting at 09am, of every day

Try this, Only if you have the same minutes for each schedule. This will run your job twice a day at 1:00 & 13:00
0 1,13 * * *
You can try quickly more variations here: https://crontab.guru/

Related

Crontab - run a command every 5th min after 9:15am

I want to run a program every 5th mins between 9:15am and 3:30pm.
5 * * * MON-FRI
seems to run the program every 5 mins (as expected) but its critical to run it only after 9:15
You must create several records in cron to accomplish what you want:
15,20,25,30,35,40,45,50,55 9 * * MON-FRI /path/to/program
*/5 10-14 * * MON-FRI /path/to/program
0,5,10,15,20,25,30 15 * * MON-FRI /path/to/program
The other way is to incorporate the logic in you program
Talking about programming way this is sample shell script which will check if you are between Monday and Friday and between 9:15 and 15:30
date +"$u %H %M"|awk '{t=$2*60+$3; if ($1>=1 && $1<6 && t>=555 && t<=930) print 1; else print 0;}'
This command will print 1 if you are in to the interval of run the things and 0 if you are outside. And then you need to run the script every 5 minutes:
*/5 * * * * /path/to/program

To run in a time window until midnight

I have an AirFlow scheduler that I want to run at 13 until midnight from Monday to Saturday. I wrote an expression like this:
0 13-0 * * 1-6
While trying to validate this in crontab.guru for example I get an error since 0 is smaller than 13:
https://crontab.guru/
Does anyone know how can I write a valid cron-expression for this type of schedule?
If you would like to run your command at minute 00 from 13:00 onwards till midnight (inclusive) on all days except Sundays, then you have to play a trick. It is not possible to define the hour 24 in a crontab. You can define the hour 00, but a crontab of the form
0 0,13-23 * * 1-6
will run on Monday 00:00 and not on Sunday 00:00 which is what the OP really wants.
Here are two methods you can use:
Run two crontabs:
0 13-23 * * 1-6
0 0 * * 2-7
Run a single crontab a minute earlier:
59 12-23 * * 1-6
How about: 0 13-23 * * 1-6
“At minute 0 past every hour from 13 through 23 on every day-of-week from Monday through Saturday.”
Source: https://crontab.guru/#0_13-23___1-6

How to create cron expression for Hangfire job that executes everyday at some time

I am new to cron expression. All i need to know that how to create cron for recurring job in Hangfire that executes after every 1 day at 5 pm, 1 am, 2:45 pm
Understanding that Hangfire also accepts standard Cron expression, I've tried exploring Cron expressions for this frequency but couldn't find one for it.
I know how it will be done for "every 15 minutes":
*/15 * * * *
I need to run it every day .
The general syntax used by cronjob schedular is :
# Execute the <b>command</b> every minute of every day.
* * * * * command
Explanation of all the fields used by cronjob schedular :
# field # meaning allowed values
# ------- ------------ --------------
# 1 minute 0-59
# 2 hour 0-23
# 3 day of month 1-31
# 4 month 1-12 (or names, see below)
# 5 day of week 0-7 (0 or 7 is Sun, or use names)
Instead of the first five fields, one of eight special strings can be used :
string meaning
------ -------
#reboot Run once, at startup.
#yearly Run once a year, "0 0 1 1 *".
#annually (same as #yearly)
#monthly Run once a month, "0 0 1 * *".
#weekly Run once a week, "0 0 * * 0".
#daily Run once a day, "0 0 * * *".
#midnight (same as #daily)
#hourly Run once an hour, "0 * * * *".
To repeat the job after an interval / is used :
*/15 * * * * command
# This will execute the command after every 15 minutes.
In order to execute the job at specific times, a "," can be used :
* 2,20 * * * command
# This will execute the job every minute but at the hours 2 AM and 8 PM.
Hope that clears your doubts.
I am Trying like:
RecurringJob.AddOrUpdate(() => Console.Write("Recurring"), "*/15 * * * *");

bash script to run everyday but not the 1st of each month

im writing more bash backup scripts but have a little problem.
on the 1st of each month i run server1.sh to do a full backup.
every other day i run server2.sh which looks at server1 backup and performs an incremental backup based on this full backup.
thats all great.
my first problem is how can i tell server2.sh to NOT run on the 1st of each month as server1.sh will be running this day.
to run these scripts im using crontab.
example cron
0 1 * * server1.sh
2 * * * server2.sh
so far i have this script using an if but its not fully working yet
#!/bin/bash
LinkDest=/home/backup/website/full
r_date=$(date "+%d-%m-%y")
f_date=$(date +%F)
c_date=$(date +%d)
servers=("123.123.78.90" "123.123.78.91" "123.123.78.92" "123.123.78.93" "123.123.78.94" "123.123.78.95" "cluster")
if [ $c_date -eq 01 ]
then
echo "Backup Skipped 1st of Month"
exit 0
else
for j in "${servers[#]}"
do
echo "server:/data/backup/$j /home/backup/website/$j"
rsync -avz --link-dest=$LinkDest root#123.123.78.90:/data/backup/"$j" /home/backup/website/$f_date --bwlimit=10000 --log-file=/logs/rsync_"$j"_"$r_date".log
fi
done
This is the format of cron expression:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * * /usr/bin/find
Given your question, you can use following cron expression:
All days except first day of the month for run server2.sh: 0 0 2-31 * *
Only first day of month for run server1.sh: 0 0 1 * *
You have to define minute and hour at which run your script. So replace first two 0 0 with your own values. For example:
20 3 2-31 * *
20 3 1 * *
To run your scripts at 3.20 AM.
Hope this help!

Cron Expression that includes from and to time

I have a difficulty writing a cron expression to schedule events Mon-Saturday every 15 minutes from 4:30 am to 8:30 am.
Thanks.
I don't think that you can solve this problem in one step, so a usable strategy might be to first coarse-filter via crontab:
0,15,30,45 4,5,6,7,8 * * 1,2,3,4,5,6 /do-whatever
which is almost OK, it will just execute 4:00 4:15 and 8:45, so we filter these at the start of the executed script:
# Too early? Then get out
if [ `date +%H%M` -lt 430 ] ; then
exit 0
fi
# Too late? Then get out
if [ `date +%H%M` -gt 830 ] ; then
exit 0
fi
# start of the original script
....
It will take 3 separate Quartz cron expressions to define the times exactly as you want them.
0 30 4,5,6,7,8 ? * MON,TUE,WED,THU,FRI,SAT *
0 45 4,5,6,7 ? * MON,TUE,WED,THU,FRI,SAT *
0 0,15 5,6,7,8 ? * MON,TUE,WED,THU,FRI,SAT *
Edited to add: This Quartz cron expression gets you from 4 am to 8:45 am, just as fvu's answer does.
0 0/15 4-8 ? * MON,TUE,WED,THU,FRI,SAT *

Resources