How to run a cron job at every 5 hour interval - linux

How to run a Cron job at every 5 hour interval?
I've tried this
0 */5 * * * /path/to/script/script.sh (Not necessarily a shell script)
But the problem with this expression is it'll run every 5 hour but the clock will reset at midnight 00:00:00 everyday.
Explanation: if the job executes at midnight 00:00:00 then following will be the execution timing
00:00:00, 05:00:00, 10:00:00, 15:00:00, 20:00:00
after 20:00:00 the job will execute at 00:00:00 which is 1 hour earlier than specified.
But my requirement is the job should run at 5 hour interval and shouldn't get reset at midnight
So if the job starts at midnight 00:00:00 the following should be the executing order.
00:00:00, 05:00:00, 10:00:00, 15:00:00, 20:00:00, (Day 1)
01:00:00, 06:00:00, 11:00:00, 16:00:00, 21:00:00, (Day 2)
02:00:00 ... (Day 3)
How do I achieve the following through Cron?
Any help would appreciated.

One option from this forum post on unix.com Run it every hour, and add to your script:
time_ct=$(cat /tmp/cron_time_ct)
if [ $time_ct -lt 5 ]
then
echo "Not yet time"
time_ct=$((time_ct+1))
echo $time_ct > /tmp/cron_time_ct
exit
else
time_ct=0
echo $time_ct > /tmp/cron_time_ct
fi
# rest of your task
I've done similar to do "every other monday" type logic. Running the script every monday and only allowing execution on every other one in the script.

Related

Cron expression to start a job the third tuesday of the month

I would like to start a job the third tuesday of the month at 03am
I try this : 0 3 15-21 * 2
crontab.guru says : At 03:00 on every day-of-month from 15 through 21 and on Tuesday
But we are today 11/24/2022 and next run is for 2022-11-29 03:00:00
I can't figure out why.
https://crontab.guru/#0_3_15-21_*_2
Thanks

how to create a cron expression for every 2 weeks

Here is a cron expression that I tried: 0 0 0 */14 * ?. It creates the following schedule:
Start Time:
Friday, September 8, 2017 1:25 AM
Next Times:
Friday, September 15, 2017, 12:00 AM
Friday, September 29, 2017, 12:00 AM
Sunday, October 1, 2017, 12:00 AM
Sunday, October 15, 2017, 12:00 AM
Sunday, October 29, 2017, 12:00 AM
This expression is working for every 2 weeks in every month, but my requirement is it has to run for every 2 weeks. I mean after executing September 29th, the next date should be October 13, but it schedules for October 1.
There is no direct cron expression for every 2 weeks. I use the following cron expression, which is similar to 2 weeks, but not exactly for 2 weeks.
cron expression for every 2 weeks on the 1st and the 15th of every month at 1:30 AM:
30 1 1,15 * *
Friday every two weeks:
0 0 * * Fri [ $(expr $(date +%W) \% 2) -eq 1 ] && /path/to/command
Found on:
https://cron.help/every-2-weeks-on-friday
30 7 1-7,14-21 * 1
β€œAt 07:30 on every day-of-month from 1 through 7 and every day-of-month from 14 through 21 and on Monday.”
You need to specify a start day. Otherwise it's will always reset with the 1st day of the month.
So this expression "0 0 0 23/14 OCT ? 2017" is every 2 weeks starting on October 23rd 2017
The crontab manual on my Ubuntu 18 says:
Note: The day of a command's execution can be specified by two fields β€” day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example,
30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result
by adding a test to the command (see the last example in EXAMPLE CRON FILE below).
and the mentioned example is:
# Run on every second Saturday of the month
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"

Combining cron schedules spread over 2 months

I have a cron task I want to schedule to run every day at 10am from 20th November to 10th December.
I'm doing this by scheduling the task twice, one from 20th November to 30th November (0 0 10 20-30 NOV ?) and again from 1st December to 10th December (0 0 10 1-10 DEC ?).
Is there any way to combine "0 0 10 20-30 NOV ?" and "0 0 10 1-10 DEC ?" together so I can have just one task and achieve the same functionality?
According to crontab.guru, these schedules can't be combined. But you could schedule it for every day and have your command make sure it's only running between those dates.
0 10 * 11,12 * TODAY=`date +"%m%d"`; [ $TODAY -ge 1120 -a $TODAY -le 1210 ] && rest_of_your_command
Today (October 23rd), the value of TODAY would be 1023 which is not between 1120 and 1210, so your command wouldn't run.

Cron Expression for ignoring 1st Saturday of the year & run on all the Saturdays of the year

Can someone help me in building the cron expression. I've a requirement where I need to run my Job on all the Saturdays at 9:00 AM in a year excluding 1st Saturday of the year.
I had tried with the below expression, but its not working..
0 0 9 ? 1 7#2,7#3,7#4 *, 0 0 9 ? 2-12 SAT *
0 0 9 ? 1 7#2,7#3,7#4 * - It means In the month of January run my job at 9:00 AM on 2nd,3rd,4th Saturdays. But the problem here is it is taking the Last saturday only (i.e. 7#4). In some tutorials I read that comma means it considers all the values but to my surprise it is considering only the last value)
0 0 9 ? 2-12 SAT * - It means from the month of February till December I need to run my job at 9:00 AM.
I need to combine these 2 expressions in such a way it should satisfy my requirement.
Thanks In Advance
You need put compare in your program, not in cronjob. For example, in Unix shell script:
#!/usr/bin/env bash
date=$(date +%a)
month=$(date +%m)
day=$($date +%e)
if [ "$date" = "Sat" -a "$month" = "Jan" -a "$day" -lt 8 ];then
echo "Doesn't run this script at 1st Saturday of the year."
exit
fi
# put rest script
set the cronjob to run it only at 9am on Saturday
0 9 * * 6 YOUR_PROGRAM
Explanation:
%a locale's abbreviated weekday name (e.g., Sun)
%b locale's abbreviated month name (e.g., Jan)
%e day of month, space padded; same as %_d

Weekly Cron Job

I want to create Cron job that will run evey 2 weeks on Sunday , I tried this but get Error bad day of week
47 15 * * SUN/2 export DISPLAY=:0 && /usr/lib/jvm/jdk1.6.0_21/jre/bin/java -jar /home/ahmed/Projects/DimensionProject/ProviderJar/FtpDownload.jar LightningSource /home/ahmed/NetBeansProjects/trunk/BookDimensionProject/build/web/linconfig.xml
so any one can help please??
We could use date +%s to obtain the number of seconds since the Epoch, convert that to weeks (604800 seconds = 1 week), and run the cron job only on odd weeks:
47 15 * * SUN test $(expr $(date +%s) / 604800 % 2) -eq 1 && echo "Every other Sunday"

Resources