1829067 Members
2612 Online
109986 Solutions
New Discussion

crontab setting

 
SOLVED
Go to solution
Terrence
Regular Advisor

crontab setting

I need to run a cronjob every other friday. Anyone know how to set this in the crontab?
7 REPLIES 7
John Poff
Honored Contributor
Solution

Re: crontab setting

Hi,

Here is one way to do it at 10:00am on the first and third Fridays of the month with two crontab entries:

0 10 1-7 * 5 /opt/mydir/somejob
0 10 15-21 * 5 /opt/mydir/somejob

If you need your job to run every other Friday, and not just twice a month, I would suggest using 'at'. You could put a line at the end of your script that does something like this:

echo "/opt/mydir/somejob" | at now +2 weeks

Run the job on Friday, and at the end it would reschedule itself to run again in two weeks.

JP

Steven E. Protter
Exalted Contributor

Re: crontab setting

If you are in need of assistance with this issues at an inconvenient time, sam under routine tasks can help you set up jobs. Then you can look at the output with crontab -e

02 6 * * 5 rm /tmp/usercount.Z

At 6: 02 a.m. every friday remove the file called /tmp/usercount.Z


SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ron Kinner
Honored Contributor

Re: crontab setting

I'd set up the crontab to run every Friday but have it run a little shell script which toggled an env variable or created or deleted a file each time it ran. Something like:

cd /home/my
if [ -f junk ]

then

# echo "doing nothing"
rm junk

else

/home/my/filetorun
echo "i just ran" > junk
fi

You may have to tinker with it a bit depending upon which shell you run but it ought to work.

Ron
Terrence
Regular Advisor

Re: crontab setting

Wow, three kings in a row.

John how does this "at" command work? Does it reset the crontab?

Thanks all!
John Poff
Honored Contributor

Re: crontab setting

The 'at' command is run by the cron daemon, but it doesn't reset crontab. It is a simple scheduler that lets you specify a command to run at a specific date and time. It is pretty flexible in the way that it lets you specify the dat/time for a job. Take a look at the man pages for all the cool things you can do with it.

JP

P.S. I'm not actually a king. I'm just the court jester. :)
john korterman
Honored Contributor

Re: crontab setting

Hi Terrance,
another possibility is to schedule the job to run every Friday in crontab, e.g.:
10 15 * * 5 /usr/local/bin/friday.sh

In the nearest future in April 2003 it would cause the script to run on the 11th, the 18th, and the 25th, and May 2.

The script itself can then check which Friday it is, based on the day number of the year, e.g.
Example made April 8, 2003:
# date %+j
098

The immediate Fridays in 2003:
April 11, will be day number 101
April 18, will be day number 108
April 25, will be day number 115

The pattern: odd-even-odd-even etc.

The script can then decide to exit on an even or an odd Friday, example:

#!/usr/bin/sh
#
# Check the day number of the year
#
typeset -i DAY_NUM=$( date +%j )
HALF=$(( $DAY_NUM \/ 2 ))
DOUBLE=$(( $HALF \* 2 ))
if [ "$DAY_NUM" != "$DOUBLE" ]
then
echo odd day
exit
else
echo even day
# exit
fi

regards,
John K.
it would be nice if you always got a second chance
Terrence
Regular Advisor

Re: crontab setting

For those keeping track, John's answer was the best and simplest soultion for my needs. But if you put the "at" command at the end of the script and the script takes time to execute, the script will constantly execute later and later on subsequent fridays. So I put it at the begining of the script.