Solved

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

Posted on 2014-01-23
16
863 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
[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
  • 4
  • 4
  • 3
  • +1
16 Comments
 
LVL 4

Expert Comment

by:tmx84
ID: 39805490
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
ID: 39805893
@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
ID: 39805897
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
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 

Author Comment

by:IT_ETL
ID: 39806544
@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
ID: 39806559
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
ID: 39806566
@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
 
LVL 8

Expert Comment

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

Author Comment

by:IT_ETL
ID: 39812511
@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
ID: 39813094
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
ID: 39824427
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
ID: 40009400
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
ID: 40009401
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
ID: 40028774
I think Surrano should get the points for http://#a39805893 (my comment http://#a39824427 was just a clarification)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
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…
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…

724 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