crontab rule

Hello.
My system FreeBSD 12.2.
Crontab rules.
# once a month, 1st day.
40 2 1 * * sh /root/cloud_YD/mount
# On Saturdays except the 1st.
40 2 2-31 * 6 sh /root/cloud_YD/saturday
# Every day, except on the 1st numbers and Saturday.
40 2 2-31 * 0-5 sh /root/cloud_YD/daily
Why did this rule work (40 2 2-31 * 6 sh /root/cloud_YD/saturday)?
It must operate only on Saturdays.
Today is Sunday.
Man crontab (day of week 0-7 (0 or 7 is Sun, or use names)), 0 or 7 is Sunday, so 6 is Saturday.
 
Day of month is *. Bitten several times by this. Read the manual carefully:. Executed when (time) AND ( day of month OR weekday)
 
I don’t understand, I’m confused.
How to start on Saturdays except the 1st?
 
I would modify the scripts, in the Saturday script, exit 0 if not Saturday. in the daily script, exit 0 if day 1 of month or Saturday.
if this is the same script, you could add that as parameter, like: -d for daily and -s for sunday.
 
We were advised to do so, we will check.
Every Saturday except the first day.
Code:
40 2 2-31 * * test `date +\%u` -eq 6 && sh /root/cloud_YD/saturday

Every day except the first day and Saturday.
Code:
40 2 2-31 * * test `date +\%u` -eq 6 || sh /root/cloud_YD/daily
 
We were advised to do so, we will check.
Every Saturday except the first day.
40 2 2-31 * * test `date +\%u` -eq 6 && sh /root/cloud_YD/saturday
Keep crontab(5) in mind, it explains the whole format. Don't use man crontab but instead man 5 crontab.

Commands are executed by cron(8) when the minute, hour,
and month of year fields match the current time, and when at least one of
the two day fields (day of month, or day of week) matches the current
time (see ``Note'' below).

Anyway, this is why all my crontab files have this header:

Code:
# m h dom month dow

So back to your time definition: 40 minutes, 2 o'clock, only on the 2-31 day of the month but then.... you tell it to run every month and every day of the month. That won't work. You also need to specify Saturday.

Every day except the first day and Saturday.
40 2 2-31 * * test `date +\%u` -eq 6 || sh /root/cloud_YD/daily
Just like your first rule this only takes the date into consideration, but not the day of the week.
 
Just like your first rule this only takes the date into consideration, but not the day of the week.
Why won't it work?
Checking what day of the week.
If true, then the command is executed.
swe@serv1 ~ $ test `date +\%u` -eq 6 || echo "today is not Saturday!"
today is not Saturday!
swe@serv1 ~ $ test `date +\%u` -eq 6 && echo "today is not Saturday!"
swe@serv1 ~ $
The rule will run from 2 to 31 every day. Checking the date, if it's Saturday then the script will run.
Every Saturday except the first day.
40 2 2-31 * * test `date +\%u` -eq 6 && sh /root/cloud_YD/saturday
Every day except the first day and Saturday.
40 2 2-31 * * test `date +\%u` -eq 6 || sh /root/cloud_YD/daily
if Saturday && will start.
If not Saturday || it will not start.
 
Code:
# On Saturdays except the 1st.
40 2 * * 6 test $(date '+%d') -ne 1 && sh /root/cloud_YD/saturday
# Every day, except on the 1st numbers and Saturday.
40 2 * * 0-5 test $(date '+%d') -ne 1 && sh /root/cloud_YD/daily

Code:
     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 (ie, are
     not *), 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.
 
# On Saturdays except the 1st. 40 2 * * 6 test $(date '+%d') -ne 1 && sh /root/cloud_YD/saturday # Every day, except on the 1st numbers and Saturday. 40 2 * * 0-5 test $(date '+%d') -ne 1 && sh /root/cloud_YD/daily
in this case, the 1st day of the month will be taken into account, and it may fall on Saturday.
Now.
#clod_YD
40 2 1 * * sh /root/cloud_YD/mount
40 2 2-31 * * test `date +\%u` -eq 6 && sh /root/cloud_YD/saturday
40 2 2-31 * * test `date +\%u` -eq 6 || sh /root/cloud_YD/daily

!!! Sorry, it seems like your version should work. !!!
 
# On Saturdays except the 1st. 40 2 * * 6 test $(date '+%d') -ne 1 && sh /root/cloud_YD/saturday # Every day, except on the 1st numbers and Saturday. 40 2 * * 0-5 test $(date '+%d') -ne 1 && sh /root/cloud_YD/daily
On a Linux system, your setup works.
The setup does not work on a FreeBSD system.
I will correct the command parameters for the FreeBSD system.
# On Saturdays except the 1st.
40 2 * * 6 test `date '+%d'` -ne 1 && sh /root/cloud_YD/saturday
# Every day, except on the 1st numbers and Saturday.
40 2 * * 0-5 test `date '+%d'` -ne 1 && sh /root/cloud_YD/daily
 
On a Linux system, your setup works.
The setup does not work on a FreeBSD system.
It works fine on FreeBSD (you're probably referring to the $(...) notation, which doesn't work with a C shell). Keep in mind that cron uses sh(1). Just because root's shell is set to csh(1) this doesn't mean that everything running on the root account is using csh(1) too.
 
40 2 * * * 6 test `date '+%d'` -ne 1 && sh /root/cloud_YD/saturday
40 2 * * * 0-5 test `date '+%d'` -ne 1 && sh /root/cloud_YD/daily
crown log
Apr 5 02:40:00 test_loc /usr/sbin/cron[14459]: (root) CMD (0-5 test `date '+)
Apr 5 02:40:00 test_loc /usr/sbin/cron[14461]: (root) CMD (6 test `date '+)

40 2 * * 6 test $(date '+%d') -ne 1 && sh /root/cloud_YD/saturday
40 2 * * 0-5 test $(date '+%d') -ne 1 && sh /root/cloud_YD/daily
crown log
Apr 5 07:34:00 test_loc /usr/sbin/cron[59625]: (root) CMD (/bin/test $(date '+)
 
Did so.
It seems to work.
40 2 * * 6 [ "$(date '+\%d')" -ne 1 ] && sh /root/cloud_YD/saturday
40 2 * * 0-5 [ "$(date '+\%d')" -ne 1 ] && sh /root/cloud_YD/daily
 
Back
Top