Crontab skip run once a week - linux

I have a CRON expression that will run a given command every 8 hours, beginning at 00:00.
0 0,8,16 * * *
This will run a given commend 21 times a week, however, my goal is to skip one of these 21 runs on a weekly basis. What is the proper CRON expression to skip the first run on Sunday each week at 00:00 (in other words, an expression that will run 20 times per week)?

Make it 2 lines:
0 0,8,16 * * 0-5 At minute 0 past hour 0, 8, and 16 on every day-of-week from Sunday through Friday.
And
0 8,16 * * 6 At minute 0 past hour 8 and 16 on Saturday.
You can change the day and hour which you want to skip, but there is no way to do this in 1 line as far as I know.

Place this: [[ ( $( date +\%u ) -ne 0 ) && ( $( date +\%H:\%M) != "00:00" ) ]] && before your command.
If you don't want to use bash for your cron-job, this works with sh:
[ $( date +\%u ) -ne 0 ] && [ $( date +\%H:\%M) != "00:00" ] &&

Related

how to set up a cronjob to run on the last sunday of each month? [duplicate]

I need to create a CRON job that will run on the last day of every month.
I will create it using cPanel.
Any help is appreciated.
Thanks
Possibly the easiest way is to simply do three separate jobs:
55 23 30 4,6,9,11 * myjob.sh
55 23 31 1,3,5,7,8,10,12 * myjob.sh
55 23 28 2 * myjob.sh
That will run on the 28th of February though, even on leap years so, if that's a problem, you'll need to find another way.
However, it's usually both substantially easier and correct to run the job as soon as possible on the first day of each month, with something like:
0 0 1 * * myjob.sh
and modify the script to process the previous month's data.
This removes any hassles you may encounter with figuring out which day is the last of the month, and also ensures that all data for that month is available, assuming you're processing data. Running at five minutes to midnight on the last day of the month may see you missing anything that happens between then and midnight.
This is the usual way to do it anyway, for most end-of-month jobs.
If you still really want to run it on the last day of the month, one option is to simply detect if tomorrow is the first (either as part of your script, or in the crontab itself).
So, something like:
55 23 28-31 * * [[ "$(date --date=tomorrow +\%d)" == "01" ]] && myjob.sh
should be a good start, assuming you have a relatively intelligent date program.
If your date program isn't quite advanced enough to give you relative dates, you can just put together a very simple program to give you tomorrow's day of the month (you don't need the full power of date), such as:
#include <stdio.h>
#include <time.h>
int main (void) {
// Get today, somewhere around midday (no DST issues).
time_t noonish = time (0);
struct tm *localtm = localtime (&noonish);
localtm->tm_hour = 12;
// Add one day (86,400 seconds).
noonish = mktime (localtm) + 86400;
localtm = localtime (&noonish);
// Output just day of month.
printf ("%d\n", localtm->tm_mday);
return 0;
}
and then use (assuming you've called it tomdom for "tomorrow's day of month"):
55 23 28-31 * * [[ "$(tomdom)" == "1" ]] && myjob.sh
Though you may want to consider adding error checking since both time() and mktime() can return -1 if something goes wrong. The code above, for reasons of simplicity, does not take that into account.
There's a slightly shorter method that can be used similar to one of the ones above. That is:
[ $(date -d +1day +%d) -eq 1 ] && echo "last day of month"
Also, the crontab entry could be update to only check on the 28th to 31st as it's pointless running it the other days of the month. Which would give you:
0 23 28-31 * * [ $(date -d +1day +%d) -eq 1 ] && myscript.sh
What about this one, after Wikipedia?
55 23 L * * /full/path/to/command
For AWS Cloudwatch cron implementation (Scheduling Lambdas, etc..) this works:
55 23 L * ? *
Running at 11:55pm on the last day of each month.
Adapting paxdiablo's solution, I run on the 28th and 29th of February. The data from the 29th overwrites the 28th.
# min hr date month dow
55 23 31 1,3,5,7,8,10,12 * /path/monthly_copy_data.sh
55 23 30 4,6,9,11 * /path/monthly_copy_data.sh
55 23 28,29 2 * /path/monthly_copy_data.sh
You could set up a cron job to run on every day of the month, and have it run a shell script like the following. This script works out whether tomorrow's day number is less than today's (i.e. if tomorrow is a new month), and then does whatever you want.
TODAY=`date +%d`
TOMORROW=`date +%d -d "1 day"`
# See if tomorrow's day is less than today's
if [ $TOMORROW -lt $TODAY ]; then
echo "This is the last day of the month"
# Do stuff...
fi
For a safer method in a crontab based on #Indie solution (use absolute path to date + $() does not works on all crontab systems):
0 23 28-31 * * [ `/bin/date -d +1day +\%d` -eq 1 ] && myscript.sh
Some cron implementations support the "L" flag to represent the last day of the month.
If you're lucky to be using one of those implementations, it's as simple as:
0 55 23 L * ?
That will run at 11:55 pm on the last day of every month.
http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
#########################################################
# Memory Aid
# environment HOME=$HOME SHELL=$SHELL LOGNAME=$LOGNAME PATH=$PATH
#########################################################
#
# 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 * * * *".
#mm hh Mday Mon Dow CMD # minute, hour, month-day month DayofW CMD
#........................................Minute of the hour
#| .................................Hour in the day (0..23)
#| | .........................Day of month, 1..31 (mon,tue,wed)
#| | | .................Month (1.12) Jan, Feb.. Dec
#| | | | ........day of the week 0-6 7==0
#| | | | | |command to be executed
#V V V V V V
* * 28-31 * * [ `date -d +'1 day' +\%d` -eq 1 ] && echo "Tomorrow is the first today now is `date`" >> ~/message
1 0 1 * * rm -f ~/message
* * 28-31 * * [ `date -d +'1 day' +\%d` -eq 1 ] && echo "HOME=$HOME LOGNAME=$LOGNAME SHELL = $SHELL PATH=$PATH"
Set up a cron job to run on the first day of the month. Then change the system's clock to be one day ahead.
I found out solution (On the last day of the month) like below from this site.
0 0 0 L * ? *
CRON details:
Seconds Minutes Hours Day Of Month Month Day Of Week Year
0 0 0 L * ? *
To cross verify above expression, click here which gives output like below.
2021-12-31 Fri 00:00:00
2022-01-31 Mon 00:00:00
2022-02-28 Mon 00:00:00
2022-03-31 Thu 00:00:00
2022-04-30 Sat 00:00:00
00 23 * * * [[ $(date +'%d') -eq $(cal | awk '!/^$/{ print $NF }' | tail -1) ]] && job
Check out a related question on the unix.com forum.
You can just connect all answers in one cron line and use only date command.
Just check the difference between day of the month which is today and will be tomorrow:
0 23 * * * root [ $(expr $(date +\%d -d '1 days') - $(date +\%d) ) -le 0 ] && echo true
If these difference is below 0 it means that we change the month and there is last day of the month.
55 23 28-31 * * echo "[ $(date -d +1day +%d) -eq 1 ] && my.sh" | /bin/bash
What about this?
edit user's .bashprofile adding:
export LAST_DAY_OF_MONTH=$(cal | awk '!/^$/{ print $NF }' | tail -1)
Then add this entry to crontab:
mm hh * * 1-7 [[ $(date +'%d') -eq $LAST_DAY_OF_MONTH ]] && /absolutepath/myscript.sh
In tools like Jenkins, where usually there is no support for L nor tools similar to date, a cool trick might be setting up the timezone correctly. E.g. Pacific/Kiritimati is GMT+14:00, so if you're in Europe or in the US, this might do the trick.
TZ=Pacific/Kiritimati \n H 0 1 * *
Result: Would last have run at Saturday, April 30, 2022 10:54:53 AM GMT; would next run at Tuesday, May 31, 2022 10:54:53 AM GMT.
Use the below code to run cron on the last day of the month in PHP
$commands = '30 23 '.date('t').' '.date('n').' *';
The last day of month can be 28-31 depending on what month it is (Feb, March etc). However in either of these cases, the next day is always 1st of next month. So we can use that to make sure we run some job always on the last day of a month using the code below:
0 8 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && /your/script.sh
Not sure of other languages but in javascript it is possible.
If you need your job to be completed before first day of month node-cron will allow you to set timezone - you have to set UTC+12:00 and if job is not too long most of the world will have results before start of their month.
If the day-of-the-month field could accept day zero that would very simply solve this problem. Eg. astronomers use day zero to express the last day of the previous month. So
00 08 00 * * /usr/local/sbin/backup
would do the job in simple and easy way.
Better way to schedule cron on every next month of 1st day
This will run the command foo at 12:00AM.
0 0 1 * * /usr/bin/foo
Be cautious with "yesterday", "today", "1day" in the 'date' program if running between midnight and 1am, because often those really mean "24 hours" which will be two days when daylight saving time change causes a 23 hour day. I use "date -d '1am -12 hour' "

Cron expression to run every day starting from a date

I need a cron expression which will fire every day at 12 pm starting from jan 25 2016. This is what I came up with:
0 0 12 25/1 * ? *
but after jan 31, the next firing time is feb 25.
Is there a cron expression expression for doing this? If not what can I use?
Assuming that after January 25th you want to run this process forever after (i.e. 2032, when probably the server will be already substituted), I would do it with three expressions:
0 0 12 25-31 1 * 2016 command # Will run the last days of Jan 2016 after the 25th
0 0 12 * 2-12 * 2016 command # Will run the rest of the months of 2016
0 0 12 * * * 2017-2032 command # will run for every day of years 2017 and after.
I hope this helps.
There are multiple ways to accomplish this task, One could be running a script with cron job and testing conditions and if true run actually required scripts otherwise skip.
Here is an example,
20 0 * * * home/hacks/myscript.sh
and in myscript.sh put your code to test conditions and run actual command/script
Here is an example for such a script,
#!/bin/bash
if( ( $(date) <= "31-01-2016" ) || ( $(date) >= "25-02-2017" ) ){
// execute your command/script
}else {
// do Nothing
}
You can write a date expression which only matches dates after a particular point in time; or you can create a wrapper for your script which aborts if the current date is before the time when the main script should run
#!/bin/bash
# This is GNU date, adapt as required for *BSD and other variants
[[ $(date +%s -d 2018-02-25\ 00:00:00) > $(date +%s) ]] && exit
exec /path/to/your/real/script "$#"
... or you can schedule the addition of this cron job with at.
at -t 201802242300 <<\:
schedule='0 0 12 25/1 * ? *' # update to add your command, obviously
crontab=$(crontab -l)
case $crontab in
*"$schedule"*) ;; # already there, do nothing
*) printf "%s\n" "$crontab" "$schedule" | crontab - ;;
esac
:
(Untested, but you get the idea. I just copy/pasted your time expression, I guess it's not really valid for crontab. I assume Quartz has a way to do something similar.)
The time specification to at is weird, I managed to get this to work on a Mac, but it might be different on Linux. Notice I set it to run at 23:00 on the previous night, i.e. an hour before the planned first execution.
This is a brief copy from my answer here.
The easiest way is to use an extra script which does the test. Your cron would look like :
# 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
0 12 * * * daytestcmd 1 20160125 && command1
0 12 * * * daytestcmd 2 20160125 && command2
Here, command1 will execute every day from 2016-01-25 onwards. command2 will execute every second day from 2016-01-25 onwards.
with daytestcmd defined as
#!/usr/bin/env bash
# get start time in seconds
start=$(date -d "${2:-#0}" '+%s')
# get current time in seconds
now=$(date '+%s')
# get the amount of days (86400 seconds per day)
days=$(( (now-start) /86400 ))
# set the modulo
modulo=$1
# do the test
(( days >= 0 )) && (( days % modulo == 0))

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 for job to run on first Sunday of the month

I have a cron line for running a command each first Sunday of the month, which looks like this:
0 19 1-7 * 0 command.sh
Minutes=0
Hours=19
Day= 1st to 7th
* = any month
0 = first day of the week (Sunday)
However, I had the surprise command.sh has run last Sunday (16th of June). Can anyone explain why my cron line is mistaken?
As an alternative, according to a wiki page, it is possible to use the "#" character to: "allow you to specify constructs such as "the second Friday" of a given month" . Is there an example available as how to use this to fit "the first Sunday of a given month"?
try this
0 0 1 ? 1/1 SUN#1 *
it will run at 1 PM every first sunday of month
Your script will run at 19, the first seven days of each month (1-2-3-4-5-6-7) AND every Sunday.
This should works in your case:
0 19 1-7 * Sun [ "$(date '+\%a')" == "Sun" ] && /some/command
Another expression which similar what Haxis mentioned below ,
0 19 1-7 * Sun [ $(date +%d) -le 07 ] && /some/command
This expression:
0 0 19 ? * 1#1 *
It will run on 19:00 on the first Sunday of every month.

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