SUSE Conversations


Easy Running of Scripts at Boot and Shutdown



By: mikewillis

May 27, 2010 2:46 pm

Reads:6586

Comments:4

Rating:2.5

In a previous Cool Solutions submission I made reference to running scripts during shutdown and boot. This is something I do on all the machines I manage. I use such scripts to perform maintenance tasks and make sure various custom configurations are in place. Here’s how I do it.

First of all you create an init script. Save this as /etc/init.d/my_maintenance

#!/bin/bash
# Author: your name here
#
# /etc/init.d/my_maintenance
#
### BEGIN INIT INFO
# Provides:          my_maintenance
# Required-Start:    network
# Should-Start:      $null
# Required-Stop:     xdm
# Should-Stop:	     $null
# Default-Start:     5
# Default-Stop:      5
# Short-Description: Runs various maintenance scripts.
# Description:       Not actually a service at all.
### END INIT INFO

. /etc/rc.status

rc_reset

case "$1" in
   start)
     # use colour for ease of spotting
      echo -e "\E[36mRunning $0 (start)...\E[0m";
      /etc/init.d/my_maintenance.d/start
      echo -e "\E[36mDone $0 \E[0m";
   ;;
   stop)

      echo -e "\E[36mRunning $0 (stop)...\E[0m";
      /etc/init.d/my_maintenance.d/stop
      echo -e "\E[36mDone $0 \E[0m";
   ;;
   restart)
      $0 stop
      $0 start
      rc_status
      ;;
   *)
      echo "Usage $0 (start|stop|restart)"
      exit 1; 
      ;;
esac 

rc_exit

Give it executable permission

$ chmod u+x /etc/init.d/my_maintenance

The Required-Start line specifies that network service should be up and running before the script is run during boot and the Required-Stop line indicates that the xdm service should have been stopped before the script is run during shutdown.

Create two directories

$ mkdir -p /etc/init.d/my_maintenance.d/start.d
$ mkdir -p /etc/init.d/my_maintenance.d/stop.d

You put scripts you want to run during boot in start.d and scripts you want to run at shutdown in stop.d. This is a lot easier than making each script you want to run an init script.

Now create the /etc/init.d/my_maintenance.d/start script

#!/bin/bash

# run scripts with names starting 0-9 in foreground. if you want to
# put a script in start.d and you care about when it gets run in relation
# to other scripts, give it a name starting 0-9
for i in $(dirname $0)/start.d/[0-9]*;do
   test -x $i && echo -e "\E[36mRunning ${i} \E[0m" && $i
done

# run scripts with names starting a-z in the background 
# as this reduces the over all time this script takes to run.
for i in $(dirname $0)/start.d/[a-z]*;do
   test -x $i && echo -e "\E[36mRunning ${i} \E[0m" && $i &
done

# wait for children to exit
wait;

and /etc/init.d/my_maintenance.d/stop

#!/bin/bash

# run scripts with names starting 0-9 in foreground. if you want to
# put a script in start.d and you care about when it gets run in relation
# to other scripts, give it a name starting 0-9
for i in $(dirname $0)/stop.d/[0-9]*;do
   test -x $i && echo -e "\E[36mRunning ${i} \E[0m" && $i
done

# run scripts with names starting a-z in the background 
# as this reduces the over all time this script takes to run.
for i in $(dirname $0)/stop.d/[a-z]*;do
   test -x $i && echo -e "\E[36mRunning ${i} \E[0m" && $i &
done

# wait for children to exit
wait;

Give them both execute permission

$ chmod u+x /etc/init.d/my_maintenance.d/start
$ chmod u+x /etc/init.d/my_maintenance.d/stop

You’ll notice that the start and stop scripts are virtually identical, so much so that they could in fact be merged and called with an argument specifying start or stop. I have them as two scripts because the versions I actually use have a few extra bits in which I’ve omitted because they’re specific to my set up.

Finally, active the init script.

$ insserv /etc/init.d/my_maintenance

Here’s an example of a script I run during boot.

#!/bin/bash
# ensure NIC is left in state to allow turning machine back on with wake on lan packet
# some drivers don't enable wol even though they and the card support it.

ethtool -s eth0 wol g

VN:D [1.9.22_1171]
Rating: 2.5/5 (2 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
Easy Running of Scripts at Boot and Shutdown, 2.5 out of 5 based on 2 ratings

Tags:
Categories: Enterprise Linux, SUSE Linux Enterprise Server, Technical Solutions

Disclaimer: As with everything else at SUSE Conversations, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.

4 Comments

  1. By:peterhine

    Mike, that’s good. Here the version i use. This just runs every .sh in /etc/server_check.d/
    in alphabetical order (so put numbers in front of them in you them to run them in a given order).


    cat /etc/init.d/server_check
    #!/bin/sh
    #
    # Manage server to start or stop services after server reboot
    #

    # chkconfig: 2345 76 54
    # description: Court server processes
    # processname: server_check
    # pidfile:
    # config:

    ### BEGIN INIT INFO
    # Provides: server_check
    # Required-Start: $local_fs $network
    # Should-Start:
    # Required-Stop: postfix
    # Default-Start: 2 3 5
    # Default-Stop: 0 1 4 6
    # Short-Description: Court server processes
    # Description: Court server processes
    ### END INIT INFO

    ulimit -c unlimited

    userID=`id`
    userID=`echo $userID | sed -e "s/(.*//" | sed -e "s/.*=//"`
    if [ "$userID" -ne 0 ]; then
    echo "$0: must be run as root."
    exit 2
    fi

    # First reset status of this service
    . /etc/rc.status
    rc_reset

    rc=0

    #echo -n "Running server_check ($1) ..."

    if test -d /etc/server_check.d ; then
    for s in /etc/server_check.d/*.sh ; do
    rc_reset
    echo -n "Running '`basename $s` ($1)' ... "
    # test -r $s && . $s $1 `basename $s`
    $s $1 `basename $s`
    rc_status -v
    done
    unset s
    fi

    #rc_status -v
    rc_exit $rc

  2. By:bbendily

    You can also write your scripts and then execute them from
    /etc/init.d/after.local
    and
    /etc/init.d/halt.local

    The after.local script will be run after all the run levels have completed, and
    the halt.local script will run before any of the run level shutdown scripting starts.
    after.local doesn’t exist by default, but it will get executed by /etc/init.d/rc if it does exist.
    Additionally, if you want to run something before any of the runlevels start, you can put it in
    /etc/init.d/boot.local
    and/or
    /etc/init.d/before.local
    After looking at these last two, i’m not really sure what the difference between them are. They appear to have the same function.
    Cheers.
    BB

  3. By:logesh44467

    all the steps are working

    cheers

  4. By:peterhine

    bbendily,

    thanks for halt/start.local

    in sles 11, if you reboot during a scripted install’s script, the install runs the script again after the reboot, and then reboots …..
    after.local solved this at just the right time i needed it.

    p

Comment

RSS