Schedule cronjob to copy files to different location every week - linux

I need help to schedule a cron on my server to copy a specific files to different locations every week in a month.
Here's what I'm trying to achieve,
Week 1 - Copy to location A
Week 2 - Copy to location B
Week 3 - Copy to location C
Week 4 - Copy to location D
The job will then repeat itself every month and I'm planning to use cp -rf command to overwrite the old files.
Could someone shed some light on how can I achieve this?

There are many ways to achieve this, basically you could use many declarations in your crontab, one for each week, or a single call to a script that would decide based on the current date.
Follows an example of how to make different cron calls every week:
0 0 1 * * cp -fr /path/to/file /path/to/destination/A
0 0 8 * * cp -fr /path/to/file /path/to/destination/B
0 0 15 * * cp -fr /path/to/file /path/to/destination/C
0 0 22 * * cp -fr /path/to/file /path/to/destination/D
You may check the expected running datetime here:
https://crontab.guru/#0_0_1__
https://crontab.guru/#0_0_8__
https://crontab.guru/#0_0_15__
https://crontab.guru/#0_0_22__

The below example will run the cronjobs at 1 AM server time on the 1st, 8th, 15th and 22nd of each month.
0 1 1 * * rsync -avz /path/to/directory /path/to/destination/A
0 1 8 * * rsync -avz /path/to/directory /path/to/destination/B
0 1 15 * * rsync -avz /path/to/directory /path/to/destination/C
0 1 22 * * rsync -avz /path/to/directory /path/to/destination/D
The format for cronjobs is:
+---------------- 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
I'd recommend using rsync instead of the cp command. If there are a lot of files involved, you can also create a ZIP or Tar before performing the copy.
Thank you!

Related

Bad hour in crontab file

I am trying to run a scheduled job via crontab in linux mint. For that, I am using the crontab -e command to edit a crontab file with the following information:
0 50 * ? * * * sh test.sh
After which I get the error:
"/tmp/crontab.XCXmSA/crontab":22: bad hour
errors in crontab file, can't install.
I tried searching but couldn't find anything that solved the problem. Tried a bunch of different times and still nothing. Any ideas?
You use totally wrong syntax. You add more stars. And questionmark which is not accepted there. Here is the syntax you search:
50 * * * * sh test.sh
And as mentioned in comments you cant have 50 as hour definition
And instead of using explicit shell add it in shebang and make the script executable
You put 50 as an hour. Hour should be in 0..23 range.
# For details see man 4 crontabs
# 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) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

CRON with AND relationship between mday and wday

I have seen this question which indicates that the relationship between the wday and mday fields of a CRON schedule is an OR relationship. Say for example I want to schedule something for every Friday the 13th.
Rather than the expected result, the CRON
0 0 13 * 5
will give me all Fridays of every month, as well as every 13th of every month.
Is there any way to avoid this behavior and specify an AND relationship? (There seems to be mention of older versions using an AND relationship, however I would prefer to use a single tool with the ability to do both)
I guess, instead of specifying the wday (Friday=5), you'll just have to specify the months where the 13th is a Friday; so, for 2019:
0 0 13 9,12 * : do stuff, but avoid black cats
Or, eternally more elegant, create a small script:
$> cat /home/me/bin/test_friday_13
#!/bin/bash
if [ "$(date +'%A %d')" != "Friday 13" ]
then
exit 1
else
: do stuff, but avoid black cats
exit 0
fi
and make the crontab entry:
0 0 13 * * /home/me/bin/test_friday_13
The script approach has the added benefit that you can run it from the command line. (Note: do not forget to alter the weekday name in the script to reflect your environment.)
The cron-entry you are interested in is:
# 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 0 13 * * [ $(date "+\%u") = "5" ] && command
This will execute the cronjob every 13th of the month. It will compute the day of the week, and test it if it is a Friday (5). If so, it will execute command.
Extended explanation:
If you want to have special conditions, you generally need to implement the conditions yourself with a test. Below you see the general structure:
# 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 0 13 * * /path/to/testcmd && command
The command testcmd generally returns exit code 0 if the test is successful or 1 if it fails. If testcmd is successful, it executes command. A few examples that use similar tricks are in the following posts:
Linux crontab for nth Satuday of the month
Is it possible to execute cronjob in every 50 hours?
Cron expression to run every N minutes
how to set cronjob for 2 days?
The test you want to perform is written in /bin/sh as:
[ $(date "+%u") = 5 ]
Which makes use of the test command (see man test). This is POSIX compliant and will work with any shell that your cron might run in (sh,bash,dash,ksh,zsh). The command date "+%u" returns the weekday from 1 to 7 (Sunday is 7). See man date for more info.
So your cronjob would look like:
0 0 13 * * [ $(date "+\%u") = 5 ] && command
Note that we have to escape the <percent>-character.
A % character in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
source: man 5 crontab
Unfortunately, CRON doesn't work the way you want
You can see all available options on below url
https://www.freeformatter.com/cron-expression-generator-quartz.html
So what you can do is the execute the cron on every 13th of the month but don't let the command run when its Friday
0 0 0 13 * ? * mybatchjob.sh
mybatchjob.sh
if [ `date +'%A'` = "Friday" ]; then
echo "Yep its Friday";
else
echo "Not Friday";
fi
This will make sure that the intended program only runs on Friday and the 13th

Crontab Centos Run Perl every 2 Minutes

i have a server RHEL 7 where i must run a script "perl" in every 2 minutes , all hour , all day , all week .
so i made theses steps ,
nano /etc/crontab
and inserted this command below .
2**** projop /web/projop/packages/intranet-helpdesk/perl/./import-pop3.perl
But is not running . this is my full crontab :
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# 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) OR sun,mon,tue,wed,thu,$
# | | | | |
# * * * * * user-name command to be executed
#29 3 * * * /root/bin/export-dbs > /var/log/postgres/export-dbs.log 2>&1
#24 3 * * * /usr/bin/find /export/backup -name '*.tgz' -mtime +6 | xargs rm
#25 3 * * * /usr/bin/find /export/backup -name '*.bz2' -mtime +6 | xargs rm
2**** projop /web/projop/packages/intranet-helpdesk/perl/./import-pop3.perl
someone could help me ? thanks.
If projop is a command call to a program with +x right and referenced into the PATH or profile or bashrc, be sure about this command and try to test it.
2 * * * * projop /web/projop/packages/intranet-helpdesk/perl/./import-pop3.perl
regards

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.

Resources