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

How to add scripts in Linux system start and shutdown scripts?

 
SOLVED
Go to solution
Gary L
Super Advisor

How to add scripts in Linux system start and shutdown scripts?

Hi

I got a task, setup OEM agent start and stop scripts into Linux system start/stop process, let Linux system automatic run OEM start script after system boot up and automatic run OEM stop script before system shutdown or reboot.

How to do that?

There are over 10 RHEL AS 4 update 6 servers installed oracle DB and OEM agnent.

I just know RH linux start script in /etc/rc.d/rc and /etc/rc.d/init.d ... where is stop script and how to accomplish my goal?

Any answers will be very appreciate.

-G
9 REPLIES
Autocross.US
Trusted Contributor
Solution

Re: How to add scripts in Linux system start and shutdown scripts?

Have a look at the scripts in /etc/init.d/

The files in /etc/rc#.d/ are links to these scripts, in most cases.

Basically, you create a script in /etc/init.d/ with start and stop options.

To have your script start, (run level 3, for ex.), create a symbolic link in /etc/rc3.d:

- cd /etc/rc3.d/
- ln -s ../init.d/scriptname S##scriptname

the rc script will start all scripts with a capital S in numerical order.

The shutdown portion is similar. Go to the run level that you want to shutdown at:

- cd /etc/rc3.d/
- ln -s ../init.d/scriptname K##scriptname

the rc script will shutdown all scripts with a capital K in numerical order.

Hope this helps,
I drive way too fast to worry about calories.
Gary L
Super Advisor

Re: How to add scripts in Linux system start and shutdown scripts?

Hi Autocross

Thanks a lot for your fast reply and good answers.

Have a good day.

-Gary
Gary L
Super Advisor

Re: How to add scripts in Linux system start and shutdown scripts?

Hi Autocross

One question for you, whether run level 3 just a command line xterm mode and run level 5 we could have GUI interface X? If so, if I wanna set system auto run the S script when system boot up to both run level 3 and 5 and auto run the K script before system shutdown/reboot at both run level 3 and 5, do I need set the link #ln in rc3.d and rc5.d?

As we know run-level 0 is halt, 6 is reboot, for K shutdown script, do I need set it in rc0.d and rc6.d.

Thank a lot.

-Gary
Matti_Kurkela
Honored Contributor

Re: How to add scripts in Linux system start and shutdown scripts?

In a nutshell:
You should create one combined startup/shutdown script, so that the script will start up the application when called with a command like:

<scriptname> start

and stop it when called with:

<scriptname> stop

This script must be placed in /etc/init.d.
After this, you must create symbolic links as Autocross.US described.

NOTE: in RHEL, there is a nice little "chkconfig" tool that helps in creating/deleting/changing the symbolic links. It requires that you put two specially-formed comment lines in your application startup/shutdown script:

# chkconfig: 345 80 20
# description: some text describing your app

The numbers on the chkconfig line:
- The first number (345) is a list of runlevels you'll want your application to run. 3 is the standard runlevel when running without GUI on the console, 5 is with the X Window System GUI. 4 is a "spare" runlevel you can define yourself if you need a special mode for your system. It is not used by default, but the convention is to keep it configured about the same as runlevels 3 and 5.

- 80 is the "starting position" number, meaning that this application will be started rather late in the boot process. You should choose this number according to the needs of your application: if you try to make the application start too early, all disks may not be mounted yet, network adapters might not be initialized or some system services might not be started yet.

- 20 is the "shutdown order" number. Unless your application has special requirements, it is recommended that the sum of startup and shutdown order numbers for each application should be 100.

When you have added the two "magic" comment lines to your startup/shutdown script, just place the script to /etc/init.d and run:

chkconfig --add <scriptname>

If you need to temporarily block the system from starting/stopping the application automatically, you can use:

chkconfig <scriptname> off

To restore the auto-start function, use:

chkconfig <scriptname> on

If you want your script to display a cool green OK message at startup and shutdown, your script should use certain pre-defined functions (this is RedHat only; other Linux distributions may do things very differently). There is usually an example script "skeleton" named /etc/init.d/skel; make a copy of it and flesh it out.

MK
MK
Gary L
Super Advisor

Re: How to add scripts in Linux system start and shutdown scripts?

Hi Matti Kurkela

That's great!

Thank you very very much for your above detail explanations, include: chkconfig: 345 80 20, example skel ...

I will try it soon.

Thanks a again.

Have a good day.

-Gary
Gary L
Super Advisor

Re: How to add scripts in Linux system start and shutdown scripts?

Hi MK

Question for you,

I havn't found the example script that you metioned above. I didn't find any /etc/init.d/skel and skeleton. In HP-UX skel is a directory for setting normal user's profile etc... not a executable script.

Any comments?

But anyway, I could find another in /etc/init.d.

Thanks a lot.

-Gary
Fredrik.eriksson
Valued Contributor

Re: How to add scripts in Linux system start and shutdown scripts?

Hi Gary

Skel is something that describe how the basics of something works.
Skel in user handling just tells us how the users environment should look when we create a user.

A init.d skel file would have "examples" that show you how to write the basics of a startup script or atleast enough for you to copy and then modify it in a way you seem fit.

When it comes to startup scripts pretty much every script in init.d looks the same.

And you're right above (didn't see any answers above). rc#.d is the runlevel. As Autocross described above you put your S##script and K##script in the rc#.d to set the runlevel.

So if you want to run it in both 3 and 5 you'd have to put in the ln in both. This thou would probably make it start twice unless you write some kind of support checking if the application exists.
Normally I usually write a pid file.
#!/bin/bash
/path/to/program & # Launch it to the background
pid=$!
echo "$pid" > /path/to/pids/program-$pid

This is a really simplistic version, normally you use "case" to make it start and stop. This also makes it easier to kill the process during your stop procedure :)

Hope I didn't rant too much again :P

Best regards
Fredrik Eriksson
Matti_Kurkela
Honored Contributor

Re: How to add scripts in Linux system start and shutdown scripts?

Sorry, looks like I confused RedHat and Debian in my memory... *Debian* has /etc/init.d/skeleton, RedHat apparently has done it a bit differently.

See the directory /usr/share/doc/initscripts-. In RHEL ES 4 Update 7 at least, there is a file named "sysvinitfiles". It contains the "skeleton" of a startup/shutdown script and an explanation of available pre-defined functions.

Note that the documentation file originates from the old RedHat Linux 7.0, but RedHat's startup script system has not changed much since then: only the old RedHat specialty of using the directory /etc/rc.d/init.d instead of /etc/init.d has been removed, just as described in the beginning of the file.

MK
MK
Gary L
Super Advisor

Re: How to add scripts in Linux system start and shutdown scripts?

Hi Fredrik Eriksson and MK

Thanks a bunch for your all kindly help and detail explanation.

-Gary