Solved

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

Posted on 2014-01-23
16
708 Views
Last Modified: 2014-04-29
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.

Currently, below scheduled job is not kicking off at 5 am. I am manually kicking this job off. Please advise what changes I need to make to below schedule job.

00 05 * * 2-5 [[ "$(date "+\%d")" != "01" ]] && /export/home/adwadm/analytics/bin/dly_res_gain_loss_dtl.sh 2&>1
0
Comment
Question by:IT_ETL
  • 4
  • 4
  • 3
  • +1
16 Comments
 
LVL 4

Expert Comment

by:tmx84
Comment Utility
Your schedule would be...

0 5 2-31 * 2-5 Your Command..

0 5 - 5am

2-31, doesn't run on the first day of the month

2-5 - Tues-Friday

Then whatever command you want it to run...
0
 
LVL 8

Accepted Solution

by:
Surrano earned 500 total points
Comment Utility
@tmx84: sorry but I think you're wrong. According to crontab(5) this will cause execution on 2-31 (every day) of each month *plus* execution on 1st of month if it falls between Tue-Fri.

I think problem is with the shell chosen. Please provide more info:
- OS name and version
- Cron daemon version
- Users and commands you used to edit your crontab

1. Try plain sh (not bash) compatible version:
[ "`date "+%d"`" != "24" ] && ...

Open in new window


2. Try creating a separate shell script, e.g. drgld_cron.sh:
#!/bin/bash
PATH="$PATH:/bin:/usr/bin"
if [[ "$(date "+%d")" != "01" ]]; then
  echo "executing script"
  /export/home/adwadm/analytics/bin/dly_res_gain_loss_dtl.sh 2&>1 
  ret=$?
  echo "script return value: $ret"
  exit $ret
else
  echo "first day of month, skipping script"
fi

Open in new window

with cronjob like this:
0 5 * * 2-5  /path/to/drgld_cron.sh >> /path/to/drgld_cron.log 2>&1

Open in new window

Then check /path/to/drgld_cron.log (use a path where you have write permissions)
0
 
LVL 8

Expert Comment

by:Surrano
Comment Utility
3. Check your server time as *same* user. Maybe that user has a different time zone setting or similar.

4. Do other cron jobs work for *same* user? For *other* users?

5. Does the same happen regardless whether you use a user-specific crontab like /var/spool/crontabs or system-specific like /etc/cron.d?
0
 

Author Comment

by:IT_ETL
Comment Utility
@tmx84,

"Your schedule would be...

0 5 2-31 * 2-5 Your Command.."

I tried this option before but it does not work as job kicks off everyday.
0
 
LVL 4

Expert Comment

by:tmx84
Comment Utility
Yep I was wrong, I forgot with those two columns they will override each other rather then work together... So you would want to do everything but 2-32, and then use your line of code to check for the first of the month and if so end etc... Sorry for the wrong response...
0
 

Author Comment

by:IT_ETL
Comment Utility
@Surrano,

I didn't quite get this one. Why 24??

[ "`date "+%d"`" != "24" ] && ...

If above doesn't work then I will consider below option,

0 5 * * 2-5  /path/to/drgld_cron.sh >> /path/to/drgld_cron.log 2>&1
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:Surrano
Comment Utility
sorry that was test data (read: today :) ). Should be 01 not 24 :)
0
 

Author Comment

by:IT_ETL
Comment Utility
@Surrano

I think problem is with the shell chosen. Please provide more info:

#!/usr/bin/bash

3. Check your server time as *same* user. Maybe that user has a different time zone setting or similar.

4. Do other cron jobs work for *same* user? For *other* users?

5. Does the same happen regardless whether you use a user-specific crontab like /var/spool/crontabs or system-specific like /etc/cron.d?

--None of these are causing issues as other cron jobs are running as usual for same user on same server

The job is scheduled using crontab -e like below,

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

Do you notice any syntax error?

You below option may work well but I do like to run one shell script instead of running multiple scripts,

#!/bin/bash
PATH="$PATH:/bin:/usr/bin"
if [[ "$(date "+%d")" != "01" ]]; then
  echo "executing script"
  /export/home/adwadm/analytics/bin/dly_res_gain_loss_dtl.sh 2&>1
  ret=$?
  echo "script return value: $ret"
  exit $ret
else
  echo "first day of month, skipping script"
fi

Any suggestions how I can make this one work,


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

Expert Comment

by:Surrano
Comment Utility
Hello IT_ETL,

When mentioning the shell chosen I did not mean what's inside the script invoked from crontab; I meant the shell that interprets crontab commands. In Solaris, it will be /bin/sh, no matter what your user's shell is. That is, the [[ ... ]] structure is analysed by /bin/sh not /usr/bin/bash or whatever is in the first line of dly_res_gain_loss_dtl.sh.

Therefore please try the plain sh-compatible version with [ ... ] instead of [[ ... ]], backticks instead of $(...) and without backslash in front of % sign.

As for the script I wrote as drgld_cron.sh it is a failsafe wrapper for your script and that will be the only single script in the crontab (without the [[ ... ]] structure); no multiple scripts.

If you tried both the sh-compatible version and the wrapper script then please check the mailbox of the same user; it should contain the output of the cron command, if any.
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
Three reasons for writing a script:
- % in a crontab line could be interpreted as a "read the next lines as stdin to my command" (escaping it is usually sufficient, but someone might come along later and delete the \ as not required)
- there is a maximum command length in crontab, and it's very short (much shorter than at a shell prompt)
- as Surrano says, the shell interpreting the lines in a crontab file is not under your control

So you can do much more in a shell script, in the shell of your choice, without having to worry about those problems.
0
 

Author Comment

by:IT_ETL
Comment Utility
I've requested that this question be deleted for the following reason:

I haven't tried this solution yet.
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
I think we've answered the question, so please leave it open until you *do* get a chance to try it.
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
I think Surrano should get the points for http://#a39805893 (my comment http://#a39824427 was just a clarification)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

10 Experts available now in Live!

Get 1:1 Help Now