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
IT_ETLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tmx84Commented:
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
SurranoSystem EngineerCommented:
@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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SurranoSystem EngineerCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

IT_ETLAuthor Commented:
@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
tmx84Commented:
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
IT_ETLAuthor Commented:
@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
SurranoSystem EngineerCommented:
sorry that was test data (read: today :) ). Should be 01 not 24 :)
0
IT_ETLAuthor Commented:
@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
SurranoSystem EngineerCommented:
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
simon3270Commented:
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
IT_ETLAuthor Commented:
I've requested that this question be deleted for the following reason:

I haven't tried this solution yet.
0
simon3270Commented:
I think we've answered the question, so please leave it open until you *do* get a chance to try it.
0
simon3270Commented:
I think Surrano should get the points for http://#a39805893 (my comment http://#a39824427 was just a clarification)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.