Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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

8 Experts available now in Live!

Get 1:1 Help Now