1830779 Members
11945 Online
110016 Solutions
New Discussion

Re: crontab job

 
ust3
Regular Advisor

crontab job

I want to setup a crontab job that run on monday , tuesday and 1st of every month , 07:00 , I have set " 00 07 1 * 1,2 my_script " , is it correct ? assume next monday is also 1st of the month ( meet both condition ) , will it duplicate run my_script ?

thx
13 REPLIES 13
Gabriel Suchter
Advisor

Re: crontab job

Hi,

yes it seems correct.
And if 1st of the month will be on monday or tuesday then the script will run only once.

Short extract from cron manpage:

When a command is specified to run at an ambiguous time, the command is executed only once at the first occurrence of the ambiguous time.

Gabriel
Oviwan
Honored Contributor

Re: crontab job

make two entries:

00 07 1 * * my_script #1st of mont
00 07 * * 1,2 my_script #monday and tuesday

Regards
Dennis Handly
Acclaimed Contributor

Re: crontab job

>Oviwan: make two entries:

This will do it twice.
Better to let it OR as Gabriel said.
(A comment may be helpful if you think the OR is tricky.)
Yogeeraj_1
Honored Contributor

Re: crontab job

hi,

> " 00 07 1 * 1,2 my_script "
This mean run my_script only when "Day of month" = 1 AND "Day of week" is Monday or Tuesday.
Of course, not what you want.

>>Oviman
00 07 1 * * my_script #1st of month
00 07 * * 1,2 my_script #monday and tuesday

This will run twice if "Day of Month" is 1 and it is ALSO either monday or tuesday.


What you need:
==============
#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
#*******************************************************************************
00 07 1 * * my_script #1st of month
00 07 * * 1,2 [ ! $(date +%e)=1 ] && my_script #run if not 1st
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************


kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: crontab job

hi again,

Sorry missed the explanations.

00 07 1 * * my_script #1st of month
00 07 * * 1,2 [ ! $(date +%e)=1 ] && my_script #run if not 1st

The first line:
Run every 1st day of the Month

The Second line:
Run every monday and tuesday, except if either is the 1st day of the month -- taken care by the 1st line.


Hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Dennis Handly
Acclaimed Contributor

Re: crontab job

>yogeeraj: 00 07 1 * 1,2 my_script
>This means run my_script only when "Day of month" = 1 AND "Day of week" is Monday or Tuesday.

That's NOT what the man page says. Only tztab does AND. crontab(1) says:
If both are specified in an entry, they are cumulative.
Yogeeraj_1
Honored Contributor

Re: crontab job

hi Dennis,

Thanks a lot for these clarifications. I really had missed that!

Below the quote from "man crontab":
===========================================
Note that the specification of days can be made in two fields: monthday and weekday.

If both are specified in an entry, they are cumulative. For example,

0 0 1,15 * 1 command

runs command at midnight on the first and fifteenth of each month, as well as every Monday.
===========================================

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: crontab job

to summarise:

this should work fine for you:

00 07 1 * 1,2 my_script

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
ust3
Regular Advisor

Re: crontab job

thx all replies,

so what I need to set ? my above setting is correct ? or I need to set as below ? thx.

00 07 1 * * my_script #1st of month
00 07 * * 1,2 [ ! $(date +%e)=1 ] && my_script #run if not 1st
Kapil Jha
Honored Contributor

Re: crontab job

Just a better way to work with cron
Put these line in crontab files.
#minute (0-59),
#| hour (0-23),
#| | day of the month (1-31),
#| | | month of the year (1-12),
#| | | | day of the week (0-6 with 0=Sunday).
#| | | | | commands
0 2 * * 0,4 /etc/cron.d/logchecker

Thanks to UGU :)
Kapil
I am in this small bowl, I wane see the real world......
Dennis Handly
Acclaimed Contributor

Re: crontab job

>my above setting is correct?

Yes. I finally convinced Yogeeraj that his complex version wasn't needed.
See his:
to summarize: this should work fine for you:
ust3
Regular Advisor

Re: crontab job

thx reply,

I have one addition question , if I want run on 1st of month when if it is Mon or Tue , can advise how to set ? thx

Dennis Handly
Acclaimed Contributor

Re: crontab job

>if I want run on 1st of month when if it is Mon or Tue, can advise how to set?

You want to run on first AND ONLY if it is Mon or Tue?
You reverse what Yogeeraj had. (And fiddle with it a little. :-)

00 07 * * 1,2 [ $(date +%d) -eq 1 ] && my_script #run if 1st

Note you can also put the date check in the script so you don't clutter up the crontab.