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

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!

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))

Crontab job executed every day instead every month

Three days ago I installed the following crontab job with crontab -e:
# execute weekly
0 2 2-31 * 7 sh /home/user/folder/myscript.sh week > /home/user/.crontablog/crontab.log
It's supposed to be executed every sunday night at 2am except the 1st of the month. However it's executed every night at 2am. What's my mistake? I tried 0 instead of 7 for Sunday with the same result :/
Thank you.
Since the format of crontab is like this:
+---------------- 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
To execute it every week on Sunday irrespective of the month you need to write it like this:
0 2 * * 7 sh /home/user/folder/myscript.sh week > /home/user/.crontablog/crontab.log
first you can analyze the content of /var/log/cron, grepping for your script to see what is going on.
I suggest you use the following syntax
0 2 * * sun /home/user/folder/myscript.sh week
having given the +x permission on the script file.
Cheers
Now that you respecified your question in the comments you need to do TWO things:
as I said above, use 0 2 * * 7 to run at 2:00am every Sunday; 7 and 0 are equivalent
inside your shell script use an additional test to quietly exit on the first of the month.
That test could be as simple as
test $(date +%d) == "01" && exit
but you could of course also make it a proper if ... with more echo and verbosity.
If you want to run the script every sunday night at 2am, but only if it's not the first day in the month, then you have to use this syntax:
0 2 * * 0 /usr/bin/test $(/bin/date +\%e) -ne 1 && your_command
The test utilliy finally check if it's the first day in month and your_command is only exected if it's not.

execute crontab twice daily at 00h and 13:30

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/

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