8

I have a cron setup to execute a bash script daily at 10pm. I have another cron setup to run monthly on the 1st of the month. Both crons launch a bash script, and the only different in the bash script is the argument they pass into the underlying java program (emulating command line launch of the java program).

The problem is, I need to somehow disable the daily cron on the 1st of the month so that both don't try to run on the same day. Is this possible to do automatically?

I suppose I can create another bash script to edit the cron before the 1st then again after to set things back up, but this seems... unclean.

7
  • not all months have 31 days, wouldn't that cause a problem?
    – SnakeDoc
    Aug 29, 2013 at 22:28
  • No​​​​​​​​​​​​​ Aug 29, 2013 at 22:29
  • a good alternative to the solution offered by @suspectus
    – SnakeDoc
    Aug 29, 2013 at 22:30
  • 2
    or 2-31 for the day-of-month field.
    – ash
    Aug 30, 2013 at 4:57
  • @ash - see my answer, that's exactly what I suggested.
    – slm
    Aug 30, 2013 at 15:36

3 Answers 3

10

In a similar vein to the solution proposed by @StephaneChazelas in the comments you could specify the range of days in the 3rd field as a range for the cron that you want to run on every day besides the 1st of the month.

The following two entries would accomplish what you're after:

   0 22 1 * *      /path/to/script/1st_of_the_month.bash    
   0 22 2-31 * *   /path/to/script/every_day_except_1st.bash
4
  • actually, doesn't seem to work for me (unless i'm doing something wrong) -- formatting in comments is terrible - so see my pastbin please for what i'm getting: pastebin.com/RKvy5d4e
    – SnakeDoc
    Aug 30, 2013 at 18:32
  • oops, it should be 0 22 2-31 * * in your example. i'll edit.
    – SnakeDoc
    Aug 30, 2013 at 18:40
  • 1
    @SnakeDoc - thanks, all those columns start to blend after a while 8-)
    – slm
    Aug 30, 2013 at 18:42
  • yup I forget them too -- I set them up, then forget how I did it (good thing for the man page lol)
    – SnakeDoc
    Aug 30, 2013 at 18:57
8

The easiest way to deal with this is just have one cron entry. Modify the bash script to check the date - if it's the 1st of the month then call java program with monthly parameters, else call the program with the daily params.

Changing the script makes the process simpler as the script as a document will describe better what is happening without requiring additional information in a cron table.

The script mods could look something like this-:

if [ $(date '+%d') == "1" ];then
   # monthly code
else
   # daily code
fi
1
  • 1
    ended up going with this solution. I created another bash script with your sample code, and under monthly - it will kick off the monthly bash script (which in turn feeds the monthly argument to the underlying java program), daily kicks off the daily script, etc.
    – SnakeDoc
    Aug 29, 2013 at 22:31
0
if [ $(date '+%d') == "1" ];then
  # monthly code
else
   # daily code
fi

I think using integer comparison if more correct

date '+%d' returns 01 not 1 so using == string comparaison will be false

instead

if [ $(date '+%d') eq 1 ];then
    # monthly code
else
   # daily code
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .