SUMMARY:crontab entry for monthly server reboot

Date: Fri Nov 10 2000 - 10:15:59 CST

Thanks to all who responded (even the flamers):

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
above command into the script. Then, call your script from cron,
with the same parameters.

Vinnie German:
I know it might sound a little bit controversial but you can do a 12
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

at 0600am Nov30
at 0600am Dec31
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
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
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
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
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
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
script to reboot on every friday from the 21st through 31st of each

Crontab Entry

0 1 21-31 * 5 reboot_script

this will cause the script below to execute at 1:00 am every Friday and
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
      YEARm4=`expr $YEAR % 4`
      YEARm100=`expr $YEAR % 100`
      YEARm400=`expr $YEAR % 400`
      if [ $YEARm4 -eq 0 -a \( $YEARm100 -ne 0 -o $YEARm400 -eq 0 \) ];
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

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
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
in 30 or 31-day months,
then check to see if, for example, the day is between 25 and 31 in a
If that is the case, it could call for a reboot. Check into the
commands for the date utility, ie,

# date "+%m"

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

The only glitch this might bring in is a boot on a leap-year February,
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
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!
        '28') # 28-day months
                If Date > 21 AND Date < 29
        *) # 31-day months
                If Date > 24 AND Date < 32
Perhaps someone has something a little cleaner than this, but this is a
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
for i in `cal | awk '{print $1}'`; do
  if [ "x" != "x"$i ]; then

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

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


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:

# 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

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

Stan Pietkiewicz:
This *can* be done through cron... What you might want to look at doing
wrap the shutdown command in a short script, and invoke the script by
whenever you need to. If appropriate, you could include some tests
the script.... For instance, if a certain file exists, you could skip
reboot and continue normally.... The script could also do whatever
date/time/status checking would be necessary to ensure that it's truly
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
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

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

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

in root's crontab will reboot you on the Friday that falls between the
& 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
at the crontab entry.


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

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

U BEFORE POSTING please READ the FAQ located at
. and the list POLICY statement located at
A To submit questions/summaries to this list send your email message to:
A To unsubscribe from this list please send an email message to:
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:

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