Solved

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

Posted on 2014-01-17
4
290 Views
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/dly_res_gain_loss_dtl.sh 2&>1
0
Comment
Question by:IT_ETL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 68

Accepted Solution

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

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

Author Comment

by:IT_ETL
ID: 39789042
Hi woolmilkporc,

Thank you for quick response.

"00 05 * * 2-5 [[ "$(date "+\%d")" != "01" ]] && /export/home/adwadm/analytics/bin/dly_res_gain_loss_dtl.sh 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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
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.
0
 

Author Comment

by:IT_ETL
ID: 39789987
Thanks woolmilkproc. It makes sense.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) 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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

729 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