1829102 Members
2500 Online
109986 Solutions
New Discussion

Re: CRON Setup

 
SOLVED
Go to solution
Laurie A. Krumrey
Regular Advisor

CRON Setup

Hi All,

I need to setup two new jobs in CRON.

1. To run every Sat. night at 8:45 to
execute /filename/script.sh

2. 4am Sunday morning to 8:30pm Saturday nite
to execute at 5 mins intervals. To execute the
file below every 5 mins. in the range above.

/filename/script2.sh

How would I enter this in cron?

Review on how to set this up...It's been a while and the last time I setup cron, I load
it several times and caused problems.

TIA,

Laurie Krumrey (formerly Laurie Brown)
Happiness is a choice
8 REPLIES 8
Patrick Wallek
Honored Contributor
Solution

Re: CRON Setup

Laurie,

Your crontab entries would look as follows:

45 20 * * 6 /filename/script.sh

0,5,10,15,20,25,30,35,40,45,50,55 4-20 * * 0-6 /filename/script2.sh

Use 'crontab -e' while logged in as the appropriate user to put these entries into that users crontab file. Do a 'crontab -l' to list crontab entries.

The syntax of a line in cron is:

minute hour day-of-month month day-of-week command to run

Any field with a * in it will run for all values of that field (every minute, every hour, every day of month, every month, every day of the week). The days of the week are specified as 0-6 where 0=Sunday, 1=Monday, etc. The hour is specified as 24 hour time. Do a 'man crontab' for more information.
Joseph C. Denman
Honored Contributor

Re: CRON Setup

crontab -e
insert

45 8 * * 6 /filename/script.sh
00,05,10,15,20,25,30,35,40,45,50,55 * * * 1-5 /filename/script2.sh
00,05,10,15,20,25,30,35,40,45,50,55 00-19 * * 6 /filename/script2.sh
00,05,10,15,20,25,30,35,40,45,50,55 04-23 * * 0 /filename/script2.sh

Should do it.

...jcd...

If I had only read the instructions first??
Patrick Wallek
Honored Contributor

Re: CRON Setup

I just went back and reread the question and double checked my response. I think I messed up on your script2 setup.

What I had would go every 5 minutes from 4 AM to 8PM every day of the week. If I read correctly what you need is every 5 minutes 24 hours a day from 4AM Sunday to 8:30 PM Saturday. To do that you would need to do the following:

#The following will run the script every 5 minutes from 4:00AM till 23:55 PM Sunday
0,5,10,15,20,25,30,35,40,45,50,55 4-23 * * 0 /file/script2.sh

#The following will run the script every 5 minutes 24 Hours a day Monday - Friday
0,5,10,15,20,25,30,35,40,45,50,55 * * * 1-5 /file/script2.sh

#The following will run the script every 5 mintes from Midnight till 19:55 Saturday
0,5,10,15,20,25,30,35,40,45,50,55 0-19 * * 6 /file/script2.sh

# The following will run the script every 5 minutes from 20:00 till 20:30 Saturday
0,5,10,15,20,25,30 20 * * 6 /file/script2.sh

Unfortunately this is the only way I can think of to run the script with your time requirements.
A. Clay Stephenson
Acclaimed Contributor

Re: CRON Setup

Case 1 is easy:
as root:
1. crontab -l > /tmp/root
2. vi /tmp/root & enter the following
45 20 * * 6 /filename/script.sh
3. Save file & crontab < /tmp/root
4. Check your work with crontab -l

Case 2 is a bit more tedious:
Same crontab & vi steps as above with these
entries:
# To take care of Mon-Fri
0,5,10,15,20,25,30,35,40,45,50,55 * * 1-5 /filename/script2.sh
# Sun
0,5,10,15,20,25,30,35,40,45,50,55 4-23 * * 0 /filename/script2.sh
# Sat
0,5,10,15,20,25,30,35,40,45,50,55 4-23 * * 6 /filename/script2.sh
0,5,10,15,20,25,30 20 * * 6 /filename/script2.sh

That should do it.

If it ain't broke, I can fix that.
Joseph C. Denman
Honored Contributor

Re: CRON Setup

whoops missed on, but patrick got it.

also:

0,5,10,15,20,25,30 20 * * 6 /filename/script2.sh

If I had only read the instructions first??
Jim Moffitt_1
Valued Contributor

Re: CRON Setup

Patrick, I think you got it the first time, the requirement, to me looks as if the job is required to go from 4 in the morning Sunday, all the way till Saturday night, without interuption. Probably so that Saturday job can maybe run some kind of report????
Laurie A. Krumrey
Regular Advisor

Re: CRON Setup

Thanks Everyone it's working great...

However..I don't want cron sending email
every 5 minutes to root...Can I tell it
only to send email if there is a problem and
not to sent email other wise?

Here's what I get in root's email:

ubject: cron
Status: R

running


*************************************************
Cron: The previous message is the standard output
and standard error of one of your crontab commands:

/usr/ops/autotree/scripts/file.sh


Happiness is a choice
A. Clay Stephenson
Acclaimed Contributor

Re: CRON Setup

2 ways to deal with this:
If you are confident that your command(s) alway(s) succeeds:
Within your script:
my_cmd > /dev/null 2>>/dev/null
to redirect both stdout & stderr to /dev/null
or
Something like this:
TFILE1=/var/tmp/X$$_1.txt
my_cmd > ${TFILE1} 2>>${TFILE1}
STAT=$?
if [ ${STAT} -ne 0 -a -s $TFILE1 ]
then
cat $TFILE1
fi
rm -f $TFILE1

If the exit status of my_cmd is non-zero,
then the stdout & stderr of my_cmd is echoed
and picked up by mail from cron

You would need to do this for each separate command in your script that you desire to monitor for error or wrapper the whole thing.
If it ain't broke, I can fix that.