SUMMARY:crontab entry for monthly server reboot

From: athiel@wfubmc.edu
Date: Fri Nov 10 2000 - 10:15:59 CST


Thanks to all who responded (even the flamers):

 
====================================
Jason.Shatzkamer:
As root, do the following:
        # crontab -e
        
Add a line for the script

        0 6 * * 5 /usr/sbin/shutdown -y -g0 -i6

That should do it. If you want, you can write a script that sends you a
confirmation notice before it reboots the machine, and just incorporate
the
above command into the script. Then, call your script from cron,
instead,
with the same parameters.

====================================
Vinnie German:
I know it might sound a little bit controversial but you can do a 12
lines
entry each line corresponding to the date on that specific friday of the
month and it will be guarantee that it will reboot machine on that day.

====================================
John Leadeham:
Every day, run (via cron) a script which checks to see
whether today is the last Friday of the month and, if so,
reboots the server.

To get today's date as a number: date +%d

To get the date of the last Friday of the month:
        set `cal | cut -d' ' -f1-6`; echo $_

And compare them with test.

====================================
Andy Lee:
Given a calendar you can quickly write down the last friday in each
month say
a year
Then use "at" to setup the reboot

There is prob a devious script in perl or somesuch to generate the "at"
commands for the next X years but really ....
====================================
Dennis Riddick:
at script e.g...

at 6am + 3 weeks
diff file file2 2>&1 > newfile | mailx support_group
!

or
at 0600am Nov30
at 0600am Dec31
etc..
diff file file2 2>&1 > newfile | mailx support_group

====================================
Vince Merrell:
You can kind of cludge it with cron by specifying day of month to be
24-30
and day of week to be 5 which is Friday (based on Sunday being 0). The
downside of this solution is that on a 31 day month, if Friday is the
31st,
the reboot will happen on the 24th instead.

Other than that, your best option is to run a script every night which
applies some logic based on the number of days in the month, works out
if
it's a Friday and if it is then is it the last Friday.
====================================
Simon Millard:
One way to do this is to write a script. In it, you can test for the
day of the week, and only proceed if the day is a Friday.

To run this at the end of the month, your crontab entry should look
something like:

0 6 25-31 1,3-12 * /location/of/script
0 6 21-28 2 * /location/of/script

The first entry is for all months except february, and the second for
february. Need to watch out for leap years though.

====================================
Claudio Cuestas:
Of course crontabs is capable to do what you are looking for.

you can specify the day of the week, the day of the month, the hour, the
minutes and the command to be executed.

look into it a little harder.

====================================
John Malick:
Crontab can do this function just perfectly. See below for root's
crontab entry:

0 6 25-31 * 5 /usr/sbin/reboot or /usr/sbin/init 6
0 6 22-28 2 5 /usr/sbin/reboot or /usr/sbin/init 6

The only issue my be leap year. You only have to worry about that once
every
four years.

The 0 is minutes thus on the hour of 6 am.

The 25-31 says that only the last seven days of each month are viewed as
possible days. Think about it; there can only be one Friday during the
last
seven days of the month which would be the last Friday of the month.
The second entry is for February.

====================================
Ted Tickell:
Two options:
Use the last day of the month in cron, instead of the last friday
6 * * * 30 shutdown -y -g0 -i6

Alternately, you can create and init script to figure out the date of
the last friday of the month, and schedule an at job. man at for more
info on that.

====================================
Bruce Zimmer:
    Actually last Friday of the month is not a big problem. If you run
this
script to reboot on every friday from the 21st through 31st of each
month.

Crontab Entry

0 1 21-31 * 5 reboot_script

this will cause the script below to execute at 1:00 am every Friday and
the
script will check to see if it is the last Friday of the month.
------------------------------- Begin Script
Here ------------------------------

#! /bin/sh

DAY=`date +%d`
MONTH=`date +%m`
YEAR=`date +%Y`
case $MONTH in
   2)
      YEARm4=`expr $YEAR % 4`
      YEARm100=`expr $YEAR % 100`
      YEARm400=`expr $YEAR % 400`
      if [ $YEARm4 -eq 0 -a \( $YEARm100 -ne 0 -o $YEARm400 -eq 0 \) ];
then
         LASTDAY=29
      else
         LASTDAY=28
      fi
      ;;
   4|6|9|11)
      LASTDAY=30
      ;;
   *)
      LASTDAY=31
      ;;
esac
FIRSTDAY=`expr $LASTDAY - 6` # FIRSTDAY is the earliest day that
                               # the last Friday can be on
                               # If being run on Friday 12 July
                               # FIRSTDAY would be 25
if [ $DAY < $FIRSTDAY ]; then # This would cause an exit on the 21st
   exit 0
fi

init 6

------------------------------- End Script
Here --------------------------------

Please excuse the verbosity, but sometimes it makes the logic easier to
follow. Obviously taking out the leap year logic would make this much
shorter. In fact if you can let the system reboot twice on the rare
occasion where a leap year February ends on the 29th, you could reduce
this
to a triplet in the crontab file.

0 1 22-29 2 5 init 6
0 1 24-30 4,6,9,11 5 init 6
0 1 25-31 1,3,5,7,8,10,12 5 init 6

====================================
Alan Reichert:
crontab can't do this directly, but you can use it to fire off a script
every Friday that checks to see if it's the last Friday of the month.

Example, fire off a script that uses case statements to check for the
last
Friday
in 30 or 31-day months,
then check to see if, for example, the day is between 25 and 31 in a
31-day
month.
If that is the case, it could call for a reboot. Check into the
formatting
commands for the date utility, ie,

# date "+%m"

will return the numeric month. Run a similar command to get the numeric
day
of
the month.

The only glitch this might bring in is a boot on a leap-year February,
but
with some
more logic, that can be worked around as well.

I don't have a script to show you, but off the top of my head, here's
some
pseudo code. Keep in mind that this
would be called each Friday by a cron job:

# Does NOT check for leap years
Month = The current numeric month
Date - current numeric day
Flag = 0 # Initialize this
        
If Month is April, June, Sept, or November, Set Flag to 30
Else If Month is February, set Flag to 28

case $Flag in

        '30') # 30-day months

                If Date > 23 AND Date < 31
                ` Reboot!
                Break
                ;;
        '28') # 28-day months
                If Date > 21 AND Date < 29
                        Reboot!
                Break
                ;;
        *) # 31-day months
                If Date > 24 AND Date < 32
                        Reboot!
                Break
                ;;
esac
         
Perhaps someone has something a little cleaner than this, but this is a
way
you could do this.
Looking forward to your summary on this.

====================================
Peter Gutmann:
No, cron (and the crontab) have almost exactly that facility. If you
were to fire a script at 6:00am on every friday that checks to see if it
is the last friday then forces a reboot (with a reboot command).

So there is a way to do this, A better question would be why are you
doing this. Unix machines in general are designed to run for years with
our rebooting. Perhaps, rebooting the machine is hiding the real
solution to the problem.

Hope this helps,

====================================
Andrew Brennan:
Augy, we're doing this locally - last Sunday of the month, 8am. I'll
attach a copy of the script. The associated crontab entry is for the
script to run every Sunday, 8am ...

#! /bin/sh
PATH=/bin:/usr/bin:/usr/sbin
for i in `cal | awk '{print $1}'`; do
  if [ "x" != "x"$i ]; then
    day=$i
  fi
done

today=`date | awk '{print $3}'`

if [ $day = $today ]; then
    echo "shutdown -i 6 -g 600 -y"
    shutdown -i 6 -g 600 -y
else
    echo "dunx1 not restarted this weekend"
fi

exit

====================================
Mark Neill:
The last friday? There is no logical date command to determine this.
 
You can run a script on every friday, at 6 AM, to see if 'date + 7' is >
'last_day_of_month' (or '30' or whatever, since last_day_of_month isn't
easy to
determine either), and if so, run the reboot command.

====================================
Geoff Wild:
   You should be able to to run a script from cron.

        Here's one that I have used:

#!/bin/sh
# Non - Interactive shutdown script
# This can be run fron cron to reboot this server.
# Geoff Wild Oct 7, 1998
#
# File: /scripts/shut.r.0
#
wall /scripts/shut.r.0.msg
sleep 300
shutdown -yr 0

        shut.r.0.msg contains:

Please Log Off NOW!!! System going down for maintenance in 5 minutes...

====================================
Matthew Stier:
Not directly, no. However, with a little help from a script you can
implement
it.

The script could be run from cron at 6am each friday. The script would
then
get the current date, and test if it was the last friday of the month,
and
run 'reboot' if it is.

====================================
Stan Pietkiewicz:
This *can* be done through cron... What you might want to look at doing
is
wrap the shutdown command in a short script, and invoke the script by
cron
whenever you need to. If appropriate, you could include some tests
within
the script.... For instance, if a certain file exists, you could skip
the
reboot and continue normally.... The script could also do whatever
date/time/status checking would be necessary to ensure that it's truly
the
last Friday (or whatever other conditions you impose).
This would likely have to be invoked from root's cron, so the usual
precautions would apply....

The cron facility is powerful enough that just about anything you can
run
from a command line, can be run within cron.

====================================
Denise Naomi:
I dont know if its a good idea, but...
You can write something like
0 6 25-31 1,3,5,7,8,10,12 5 "shutdown"
That will shutdown your system in the last friday of the month...

Probably you could have 3 entries in cron to months with 28, 30 or 31
days.

====================================
Ray English:
It would require some tricky stuff to get "the last" Friday of the
month.

00 6 22-28 * 5 /usr/sbin/reboot

in root's crontab will reboot you on the Friday that falls between the
22nd
& 28th of the month (since all months have at least 28 days). If you can
reboot the first Friday of the month,

00 6 1-7 * 5 /usr/sbin/reboot

would probably make more sense to someone unfamiliar with your setup
looking
at the crontab entry.

==================================
==================================

Again, thanks to all for your rapid, insightful, helpful examples!

=================================================
Augy Thiel athiel@wfubmc.edu (336) 713-4712
=================================================
Unix - Sun - Tech Support/Analyst
Dept. Public Health Sciences
Wake Forest University Baptist Medical Center
Winston-Salem, North Carolina

S
U BEFORE POSTING please READ the FAQ located at
N ftp://ftp.cs.toronto.edu/pub/jdd/sun-managers/faq
. and the list POLICY statement located at
M ftp://ftp.cs.toronto.edu/pub/jdd/sun-managers/policy
A To submit questions/summaries to this list send your email message to:
N sun-managers@sunmanagers.ececs.uc.edu
A To unsubscribe from this list please send an email message to:
G majordomo@sunmanagers.ececs.uc.edu
E and in the BODY type:
R unsubscribe sun-managers
S Or
. unsubscribe sun-managers original@subscription.address
L To view an archive of this list please visit:
I http://www.latech.edu/sunman.html
S
T



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:14:22 CDT