How to schedule a job to run on specific days of the week using crontab?

Posted on 2014-01-17
Last Modified: 2014-01-17
I need to schedule a job that will run at 5 am from Tue-Fri, only 4 days in a week using crontab. But if Tue/Wed/Thur/Fri is the first day of the month then this job should not run. How do I accomplish that? Below scheduled job is not working as it runs on Saturday/Sunday. Please advise what changes do I need to make in a below scheduled job.

00 05 2-31 * 2-5 /export/home/adwadm/analytics/bin/ 2&>1
Question by:IT_ETL
  • 2
  • 2
LVL 68

Accepted Solution

woolmilkporc earned 500 total points
ID: 39788707
00 05 * * 2-5 [[ "$(date "+\%d")" != "01" ]] && /export/home/adwadm/analytics/bin/ 2&>1

Please note the backslash "\"  in front of "%" - it is mandatory in crontab!

Author Comment

ID: 39789042
Hi woolmilkporc,

Thank you for quick response.

"00 05 * * 2-5 [[ "$(date "+\%d")" != "01" ]] && /export/home/adwadm/analytics/bin/ 2&>1"

Would you briefly explain  [[ "$(date "+\%d")" != "01" ]] &&?

Just a note, this job should not run on Sat, Sun, and Mon. Also, if it is the first day of the month then job should not run even though the day is Tue/Wed/Thur/Fri.

Should it be [[ "$(date "+\%d")" != "601" ]], why do we need "&&" in a above schedule.

Please advise.
LVL 68

Expert Comment

ID: 39789154
Why "601"? The job should not run on the first of a month, and date "+%d" is "01" on such a day.

!= means "not equal" (string comparison) and "&&" means "run the following statement if the previous expression returns "true", which is obviously not the case on the first of a month. It's just a short form of "if ... then ...".

The job is triggered by cron only on weekdays 2 to 5, which means Tuesday, Wednesday, Thursday and Friday.
Now our check comes into play. If we have a value other than "01" for date "+%d" then we run the script, otherwise we don't run it, but exit silently.

That's all.

Author Comment

ID: 39789987
Thanks woolmilkproc. It makes sense.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now