Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding an rc script 4

Status
Not open for further replies.

sm42

Technical User
Dec 11, 2003
133
GB
Hello

I have not added an rc script for a long time now.

Can someone refresh my memory?

Thanks
 
1. Add your script to /etc/init.d/
2. ln -s /etc/init.d/your_script /etc/rc2.d/S99script
 
Depending on what the script does, will indicate where to put it... If it is a service you want to start, /etc/rc2.d or /etc/rc3.d would be a good place to put it. Remember, your start script must be with a S (example: S100myscript). If you want to shut down your service during an init or shutdown command, you will need the shutdown script that begins with K (Example: K100myscript)... You would put the shutdown script on level below the start script. In most cases the rc scripts have both the start up and shutdown code in them.
 
A few more details...

The number after the "S" or "K" should be only two digits. This is used to control the order they scripts are run in that run level. "S23script" is run before "S99otherscript".

When entering a run level, all of the "K" scripts are run first, then all of the "S" scripts. And again, they are run in the order of the two digits following the "S" or "K". You can see the order they will be run by typing this...
Code:
ls -1 /etc/rc3.d/[KS][0-9][0-9]*

The [tt]init[/tt] process is the one running these scripts. They are run in the bourne shell so you need a she-bang line (i.e. [tt]#!/bin/ksh[/tt]) if you need another shell.

When the "S" scripts are run, they get passed a single parameter of "[tt]start[/tt]". When the "K" scripts are run, they get passed a single parameter of "[tt]stop[/tt]". This allows you to have a single script that does both start and stop duties. These are usually placed in [tt]/etc/init.d[/tt] with soft links in the [tt]/etc/rc?.d[/tt] directories. Then, in the single script in [tt]/etc/init.d[/tt], you just have a case statement that checks [tt]${1}[/tt] for if it's starting or stopping.

If you're using a single start/stop script in [tt]/etc/init.d[/tt], you just need to create links in each run level directory. Say you wanted your script to ONLY be running in run level 3, then do the following...
Code:
ls -s /etc/init.d/mystartstop /etc/rc0.d/K99myserver
ls -s /etc/init.d/mystartstop /etc/rc1.d/K99myserver
ls -s /etc/init.d/mystartstop /etc/rc2.d/K99myserver
ls -s /etc/init.d/mystartstop /etc/rc3.d/S99myserver
ls -s /etc/init.d/mystartstop /etc/rcS.d/K99myserver
This will allow you to "stop" your process just by doing an "[tt]init 2[/tt]" taking the system into run level 2. You can also create custom run levels if you want a run level identical to run level 3, but without your process running. This is usually run level 4, which is unused.

Here's an example Oracle startup. It's placed in [tt]/etc/init.d/oracle[/tt]...
Code:
# Set ORA_HOME to be equivalent to the ORACLE_HOME from
# which you wish to execute dbstart and dbshut
# Set ORA_OWNER to the user id of the owner of the Oracle
# database in ORA_HOME

ORA_HOME=/oracle/8.1.6/
ORA_OWNER=oracle

if [ ! -f ${ORA_HOME}/bin/dbstart -o ! -d ${ORA_HOME} ]
then
        echo "Oracle startup: cannot $1"
        exit
fi

case "$1" in
'start')
    # Start the Oracle databases:
    echo "Starting Oracle Database"
    /usr/bin/su - ${ORA_OWNER} -c "export ORACLE_HOME=${ORA_HOME}; ${ORA_HOME}/bin/dbstart ; /usr/bin/sleep 20 ; ${ORA_HO
ME}/bin/lsnrctl start"
    ;;
'stop')
    # Stop the Oracle databases:
    echo "Shutting Down Oracle Database"
    /usr/bin/su - ${ORA_OWNER} -c "export ORACLE_HOME=${ORA_HOME}; ${ORA_HOME}/bin/lsnrctl stop ; ${ORA_HOME}/bin/dbshut"
    ;;
*)
    # Invalid parameter
    echo "ERROR: Oracle startup/shutdown error."
    echo "       Unknown parameter given to /etc/init.d/oracle \"$1\""
    ;;
esac
Then just create the links in the different run level directories as described above.

(Sorry for the lengthy reply ... I was bored) [bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top