Link to home
Start Free TrialLog in
Avatar of IT_ETL
IT_ETL

asked on

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

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
Avatar of tmx84
tmx84
Flag of United States of America image

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...
ASKER CERTIFIED SOLUTION
Avatar of Surrano
Surrano
Flag of Hungary image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Avatar of IT_ETL
IT_ETL

ASKER

@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.
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...
Avatar of IT_ETL

ASKER

@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
sorry that was test data (read: today :) ). Should be 01 not 24 :)
Avatar of IT_ETL

ASKER

@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
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.
Avatar of simon3270
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.
Avatar of IT_ETL

ASKER

I've requested that this question be deleted for the following reason:

I haven't tried this solution yet.
I think we've answered the question, so please leave it open until you *do* get a chance to try it.
I think Surrano should get the points for http://#a39805893 (my comment http://#a39824427 was just a clarification)