Easy Running of Scripts at Boot and Shutdown

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

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.

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;

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

(0 votes, average: 0.00 out of 5)You need to be a registered member to rate this post.

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.

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