System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Run shell script before server shutdown and after startup.

 
SOLVED
Go to solution
Gulam Mohiuddin
Regular Advisor

Run shell script before server shutdown and after startup.

I need to run one script before Linux Server shutdown and another script after server is up and running?

/u01/stop_tuxedo.sh

/u01/start_tuxedo.sh

Thanks,

Gulam.
Everyday Learning.
5 REPLIES
Ivan Ferreira
Honored Contributor
Solution

Re: Run shell script before server shutdown and after startup.

You can create an System V init script with start/stop/status and add it to the /etc/rc.d/init.d directory. Thenc reate the right links in /etc/rc.d/rc0.d (shutdown) and /etc/rc.d/rc[35].d (startup).

A lot of samples you will find in /etc/rc.d/init.d, you can use one as template.

Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Matti_Kurkela
Honored Contributor

Re: Run shell script before server shutdown and after startup.

The paths listed by Ivan are correct for old RedHats, but most current Linux distributions have simplified the directory structure a bit:
the scripts should be located in /etc/init.d instead of /etc/rc.d/init.d and /etc/rc0.d instead of /etc/rc.d/rc0.d respectively.

The simplest possible script would be something like this:

#!/bin/sh

case "$1" in
start)
/u01/start_tuxedo.sh
;;
stop)
/u01/stop_tuxedo.sh
;;
*)
echo "Usage: $(basename $0) "
;;
esac

Save the above script as /etc/init.d/tuxedo, then set it executable with
chmod a+x /etc/init.d/tuxedo

Then find the correct runlevel used by your distribution. It's typically 3 or 5: the exact meaning of various runlevels depends on your distribution, but runlevel 0 means "shut down and halt/power-off the system", 1 is "go to single user mode" and 6 is "shut down and restart".
Look into the file /etc/inittab. In it, you'll find a line like this:

id:3:initdefault:

The number identifies the system's default runlevel (3 in this case).

To make your script a part of normal system startup & shutdown, you'll need to add two symbolic links. If your default runlevel is 3, you'll need to add the startup link to directory /etc/rc3.d; if 5, then use directory /etc/rc5.d instead.

To add the startup link as the last action of startup of runlevel 3:

ln -s /etc/init.d/tuxedo /etc/rc3.d/S99tuxedo

(S means "start", 99 causes this script to be run after all the other actions. The main runlevel-change script runs your script with a command line like "/etc/init.d/tuxedo start")

To add the shutdown link as the first action of the shutdown procedure, you must add it to both /etc/rc0.d and /etc/rc6.d:

ln -s /etc/init.d/tuxedo /etc/rc0.d/K01tuxedo
ln -s /etc/init.d/tuxedo /etc/rc6.d/K01tuxedo

(K means "kill": it makes the main runlevel-change script to run your script with a command line like "/etc/init.d/tuxedo stop".)

Instead of making the symbolic links manually, you can usually use some tool to automatically add your script to the startup/shutdown configuration. If you're running RedHat or Fedora, there should be a nice tool named "chkconfig": read its man page for more information.

MK
MK
skt_skt
Honored Contributor

Re: Run shell script before server shutdown and after startup.

For a script to interact with chkconfig and the automatic starting and stopping of services at different run levels, it needs two key components.

The first is a commented section that allows the script to be managed by the chkconfig tool. Here is an example:

#!/bin/bash

#
# chkconfig 345 10 90
# description This is where you put a description of your service
In this example, the numbers 345 after the chkconfig directive represent the runlevels that the service will be enabled for by default. In this example the service will be enabled by default for runlevels 3, 4, and 5.

The 10 is the start priority. The lower the number the higher the priority and the sooner a service will be started. The 90 is the stop priority. The lower the number the higher the priority and the sooner a service will be stopped when changing runlevels.

The second essential component is that the script must support being called with the arguments "start" and "stop." The script will be called with "start" as an argument when the service is being started at a given runlevel and "stop" when it is being stopped. Within these functions the script should touch a lock file in the /var/lock/subsys/ folder. If this is not done, the script will automatically start, but will not stop automatically. This should be done in the following manner:

start() {
...
touch /var/lock/subsys/servicename

}

stop() {
...
rm -f /var/lock/subsys/servicename
}
Once you have created a script that conforms to these two requirements, place a copy of it into the /etc/init.d/ directory and execute the command:

chkconfig --add servicename
This will register the service and create the necessary links in the appropriate rcX.d directories. You can now administer this service using chkconfig command.

Steven E. Protter
Exalted Contributor

Re: Run shell script before server shutdown and after startup.

Shalom,

You could integrate these scripts into an existing deamon start/stop.

Edit a script in /etc/init.d

Better.

Put the scripts in a fresh script in /etc/init.d

Usually I copy cron as a template.

Then in /etc/rc#.d (# is number) softlink to the script in /etc/init.d If it starts with an S it runs at startup

If it starts with a K it runs at shutdown.

Case sensitive, small letters will do nothing.

example:
cd /etc/rc5.d
ln -s /etc/init.d/myscript S90myscript

cd /etc/rc4.d
ln -s /etc/init.d/myscript K10myscript

The above example runs the start/strop script /etc/init.d/myscript in start mode at run level 5 and stop(kill) mode at run level 4.
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
Jeeshan
Honored Contributor

Re: Run shell script before server shutdown and after startup.

Dear Gulam

place the startup script in /etc/rc.local

and you can shutdown the server using the script.
a warrior never quits