Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For cron job we know we can set time as below.

# +------------ Minute (0 - 59)
# | +---------- Hour (0 - 23)
# | | +-------- Day of the Month (1 - 31)
# | | | +------ Month (1 - 12)
# | | | | +---- Day of the Week (0 - 7) (Sunday is 0 or 7)
# | | | | |
# * * * * * command

What I want to know is that when we set day of the month 31, does this means the end of each month though the month does not have day 31. Hope to get answer.

Thanks in advance

share|improve this question

migrated from Aug 18 '09 at 12:26

This question came from our site for professional and enthusiast programmers.

No, 31 means 31.

However, you can do some trickery. Set the job to run on any day which could potentially be the last day of the month (ie 28-31 in the day-of-month field), and then replace your command with a shell expression comprising a test on the date guarding the command:

0 0 28-31 * * [ "date +%m" != "date --date=tomorrow +%m" ] && command

The expression inside the test brackets just asks if the month number today is different to the month number tomorrow, which of course will only be true on the last day of the month. Note that the form of this expression depends on your local date - you may need to tweak it if you don't have the current GNU version.

I should say that i didn't invent this - i found it with a quick google (which the original poster should have done himself, bad poster!) in a mailing list post by a Matthew Jarvis. I would imagine this is very much a standard old unix wizard's trick, though.

share|improve this answer
It can't be too old of a trick given the GNU-specific options to "date" =) – jj33 Aug 18 '09 at 14:13
On stackexchange sites, asking a question which can be answered with a Google search, is NOT a bad thing. In fact it is a good thing, as the next person who will search this in Google will find the answer on stackexchange and come here. – miernik May 15 '11 at 8:45

No, I think the best you can do is run at midnight on the 1st of the month, or set up individual crontab lines for each month (or at least, a line for months with 28,29,30 and 31 days)

0 0 28,29 2               * /my/command
0 0 30    4,6,9,11        * /my/command
0 0 31    1,3,5,7,8,10,12 * /my/command
share|improve this answer
Thanks Paul! I will follow the second one. For second method do i need to set the same file for three times right? Thanks again :) – Anonymous Aug 18 '09 at 10:22
Yes, you will need to repeat it. – Paul Dixon Aug 18 '09 at 10:53
I have no experience with cron, but wouldn't "0 0 28,29 2" execute twice in a leap year? Not that this would be a problem for the next two years, but still... – Wim ten Brink Aug 18 '09 at 14:52
That's a good point, no easy way around that – Paul Dixon Aug 21 '09 at 15:43

I don't think you can do this easily in the crontab.

One option would be to set cron to run the script every day and then make the script itself check if it is the last day of the month.

share|improve this answer

I guess cron has a special character 'L' meant for this purpose:

*    *    L    *    * 

would execute your script on the last day of every month

On the other hand,

*    *    0    *    *

You can even specify something like:

*    *    *    *    1,5L

to run the script on the last Monday and Friday of each month


*    *    *    *    1

to run the script every Monday at midnight.

share|improve this answer

This will do the job:

if [ $( date +%-d --date="tomorrow" ) -eq 1 ]
then echo Last day of the current month
else echo Not yet!
     exit 0

exit 1
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.