Solved

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

Posted on 2014-01-17
4
287 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

679 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