# crontab rule



## bagas (Apr 4, 2021)

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


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.


----------



## BjarneB (Apr 4, 2021)

Day of month is *. Bitten several times by this. Read the manual carefully:. Executed when (time) AND ( day of month OR weekday)


----------



## eternal_noob (Apr 4, 2021)

You can test your crontab rules online here: https://crontab.guru/


----------



## bagas (Apr 4, 2021)

I don’t understand, I’m confused.
How to start on Saturdays except the 1st?


----------



## trev (Apr 4, 2021)

bagas said:


> I don’t understand, I’m confused.


Your mday field is 2-31 (days 2 through 31 in the month).

So, it is executed when (time field) AND (day of month field OR weekday field).


----------



## BjarneB (Apr 4, 2021)

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.


----------



## bagas (Apr 4, 2021)

Still did not understand.


----------



## bagas (Apr 4, 2021)

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

Every day except the first day and Saturday.

```
40 2 2-31 * * test `date +\%u` -eq 6 || sh /root/cloud_YD/daily
```


----------



## ShelLuser (Apr 4, 2021)

bagas said:


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


```
# 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.



bagas said:


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


----------



## bagas (Apr 4, 2021)

ShelLuser said:


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


----------



## SirDice (Apr 4, 2021)

```
# 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
```


```
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.
```


----------



## bagas (Apr 4, 2021)

SirDice said:


> # 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. !!!


----------



## SirDice (Apr 4, 2021)

bagas said:


> Sorry, it seems like your version should work. !!!


Crontab triggers on every saterday, the test prevents it from running on the first day of the month.


----------



## bagas (Apr 4, 2021)

SirDice said:


> Crontab triggers on every saterday, the test prevents it from running on the first day of the month.


it is, overlooked your previous post.
Thank you all for your assistance.


----------



## bagas (Apr 4, 2021)

SirDice said:


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


----------



## SirDice (Apr 4, 2021)

bagas said:


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


----------



## bagas (Apr 5, 2021)

SirDice said:


> 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


Does not work!
Didn't work today.


----------



## bagas (Apr 5, 2021)

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


----------



## bagas (Apr 5, 2021)

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


----------

