Run a perl or shell script cron job on the first Monday or the Nth weekday of the month

by Vivek Gite on July 5, 2007 · 12 comments

This is a classic problem. One of our FAQ is about cron job. I received lots of email with a question:

How do I run my script on 3rd Monday or 4th Friday only?

Cron does not offer this kind of facility i.e. you cannot run a script on the Nth weekday of the month.

However with one shell liner you can force to run a script on a given day:

Consider following date command, it will print day:
$ date +%a
Output:

Thu

You can compare output with weekday name using bash test [exrp ] syntax and the control operators && (AND list), you can write:
$ [ $(date '+%a') == 'Thu' ] && echo 'Today is Thu, run a command' || echo 'Noop'

First echo command get exectued only on Thursday. Now all you have to do is write a cron job to execute on first Monday:
# crontab -e
Now append code as follows:
# Run a script called myscript.sh on First Monday at 11:30:
30 11 1-7 * Mon [ "$(date '+%a')" == "Mon" ] && /path/to/myscript.sh

Hope this small tip will save your day. Please do share some of your favorite bash / shell scripting hacks in the comments. I will highlight some of the best in next shell scripting post.

See also:

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 12 comments… read them below or add one }

1 Scott Carlson July 5, 2007

Nice trick, but the crontab is incorrect. As written it will run on all mondays. See crontab(5): “Note: The day of a command’s execution…”

# Run a script called myscript.sh on First Monday at 11:30:
30 11 1-7 * * [ "$(date '+%a')" == "Mon" ] && /path/to/myscript.sh

Reply

2 Ram March 3, 2011

Hi, I wanted to run a script first Thursday of the every month. I added the script in crontab as per your suggesition. It seems somehow it didn’t work.
syntax:

32 13 1-7 * * ["$(date '+%a')" == "Thu"]&&/warehouse/db2scripts/db2_test.ksh > /tmp/db2_test.del

Can you please let me know if i did anything wrong.

Regards,
Ram.

Reply

3 SREEKUMAR September 17, 2007

Hi,
I need to execute a script file with the crontab.
I just enter the entry in cronfile and it is not working…
But I can exectute the script manually from the same location with out any issues…

Please advice on this
# Minute Hour Day of Month Month of Year Day of Week Command
57 * * * * sudo /var/opt/R_5.0.0/WebNMS/mysql/bin/mysqlbackup.sh

Reply

4 parashar October 1, 2007

what i do to run my script automatically on last day of every month?

Reply

5 Nilay December 6, 2007

Just to share few lines regarding the problem I faced while setting up shell script thru cron …
Make sure you set at the very top of your script the directory path where you script lies, or else your script will crash using the relative path …
use …
cd `dirname $0`
This will take cron to your scripts directory and execute it form there ..hope it will save some1s time.

Reply

6 thegetpr March 12, 2008

how to add php script to cron job

Reply

7 Alan March 22, 2008

In answer to thegetpr’s question, try this to run a PHP script from cron

50 23 * * * lynx -dump http://www.example.com/scrip.php

Reply

8 radar August 2, 2008

Whoever put comment #4 it saved me a lot of time, I had a problem running the script in cron, but it did it!

Thanks a lot for your help.

Reply

9 James September 10, 2008

I should have read the responses. Post 1 is right. I set this up and discovered the same thing.

Thanks for the tip anyway though it certainly helped me.

Reply

10 Eric February 10, 2010

What is the syntax to run it every other Monday?

Reply

11 Sandeep Sonawane February 11, 2010

Hi ,
I don’t understanding why my perl script is not executing through crontab .
but it runs properly if we run it at command prompt by typing “perl $path_of_script”.
I found that cron daemon reads the line “perl $path_of_script” as command at proper timing defined in crontab and logs it in /var/log/cron.
But it is not executing.
I think it may be issue of environmental variables for cron daemon.
How to debug this issue ?
Thanks for help in advance.

Reply

12 brian June 7, 2010

Note that you must escape (\) the % in a crontab entry, otherwise cron will treat the % as a newline and feed the rest of the command line as STDIN. The correct command would be:
30 11 1-7 * * [ "$(date '+\%a')" == "Mon" ] && /path/to/myscript.sh

Thanks for the great tip

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 4 + 11 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: