Operating System - HP-UX
1834780 Members
2953 Online
110070 Solutions
New Discussion

how to start a script during the boot at run level 3

 
SOLVED
Go to solution
andrea_53
Advisor

how to start a script during the boot at run level 3

Hello, we are two italian guys who need help to solve a little problem. We would like to know how to start a script at run level 3.
We know that we have to put the link in /sbin/rc3.d but we don't know how to do it and if we need to do something else. Please help us. Thanks a lots.
16 REPLIES 16
Emiel van Grinsven
Valued Contributor

Re: how to start a script during the boot at run level 3

Hi,

It's like you said, create a link in this directory to the startup file of what you want to start.
the start files should be named
S[unique_number][command_name]

See man ln for how to use the command
(ln -s [file1] [file2])

Also you need to do the same for the kill script.
these files (used to kill the proces should be named K[unique_number][process_name]

HTH, Emiel
Alex Glennie
Honored Contributor

Re: how to start a script during the boot at run level 3

v.good info also in /usr/share/doc/start_up.txt
T G Manikandan
Honored Contributor
Darrell Allen
Honored Contributor

Re: how to start a script during the boot at run level 3

Hi,

/sbin/init.d contains the actual rc scripts. You should have one named "template" to use as an example for creating your /sbin/init.d script.

Then create a symlink in /sbin/rc3.d for starting the script and one in /sbin/rc2.d for stopping it.

Example:

cd /sbin/init.d
cp -p template myscript
make your changes to myscript
ln -s /sbin/init.d/myscript /sbin/rc3.d/S900myscript
ln -s /sbin/init.d/myscript /sbin/rc2.d/K100myscript

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Robert Gamble
Respected Contributor

Re: how to start a script during the boot at run level 3

A few other things to help you:

(1) Most scripts called at start up through the rcX.d directories are placed in /sbin/init.d, and are then linked to the appropriate rcX.d directory. Those scripts should contain a case statement at least 'start' and 'stop' as arguements.

(2) A sample script can be found at /sbin/init.d/template. That script *must* be edited a lot, but has the format you probably want to use.

Good Luck!
G. Vrijhoeven
Honored Contributor

Re: how to start a script during the boot at run level 3

Hi,

put the script in

/sbin/init.d

create start link in /sbin/rc3.d/
the name of the link has to start with a "S" followed by a number. The number you specify will adjust the startup sequence so the highest number will be started last.

# cd /sbin/rc3.d
# ln -s /sbin/init.d/<scriptname> /sbin/rc3.d/S???<scriptname>


You should also create an halt ( kill ) script to stop the software.
These scripts start with a K??? ( 1000 - Snumber).
Put a link in rc2.d:

ln -s /sbin/init.d/<scriptname> /sbin/rc2.d/K???<scriptname>

Hope this will help,

Gideon
S.K. Chan
Honored Contributor

Re: how to start a script during the boot at run level 3

Copy you script into /sbin/init.d and create a "control file" in /etc/rc.config.d
I would recommend using the template script, this can be found as /sbin/init.d/template
Now in /sbin/rc3.d for example you would create the link as such ..
# ln -s /sbin/init.d/myscript S995myscript
and in /sbin/rc2.d you would create the "kill" script link as such ..
# ln -s /sbin/init.d/myscript K199myscript
James R. Ferguson
Acclaimed Contributor

Re: how to start a script during the boot at run level 3

Hi:

If you haven't, read either version of the document below. I???ll summarize the key points here.

/usr/share/doc/start_up.txt

http://docs.hp.com/hpux/onlinedocs/os/startup.html

The file /sbin/init.d/template is a good starting place for making your own start/stop scripts.

The /sbin/init.d directory contains all scripts used to startup and shutdown various subsystems.

Each script under /sbin/init.d should perform BOTH the startup and shutdown functions. In order to control the functionality within the script, each must also support standard arguments and exit codes. Scripts must be written for the POSIX shell. A template script may be found in /sbin/init.d/template.

There is no reason why the startup and shutdown script cannot start/kill multiple, but related processes. Remember to choose the appropriate rc.d directory -- one (1) is for core services; two (2) is for multiuser run-state; three (3) is for networked, multi-user; and four (4) is for graphical interfaces. Depending on the processes you are starting, or stopping, you want to make sure prerequisite services exist.

Each script in /sbin/init.d performs BOTH the startup and shutdown functions, and each will have two links pointing towards the script from /sbin/rc*.d: one for the start action and one for the stop action.

Start scripts begin with "S"; Kill (stop) scripts begin with "K". The order of execution for kill scripts is the reverse of the startup ones.

if a start script is placed in directory '/sbin/rc{X}.d' then its corresponding kill script is put in directory '/sbin/rc{X-1}.d'

A general rule-of-thumb is that the sequence number of the start script plus the sequence number of the kill script should add to 1000.

Subsystems should be killed in the opposite order they were started. This implies that kill scripts will generally not have the same numbers as their start script counterparts. If two subsystems must be started in a given order due to dependencies (e.g., S200sys1 followed by S300uses_sys1), the counterparts to these scripts must be numbered so that the subsystems are stopped in the opposite order in which they were started (e.g., K700uses_sys1 followed by K800sys1). The '1000' rule leads to this behavior.

Regards!

...JRF...
John Strang
Regular Advisor

Re: how to start a script during the boot at run level 3

Hi Andrea,

Place your script in /sbin/init.d (have a look at other scripts in that directory to get the correct format).

Then make a link to your script from /sbin/rc3.d (for run-level 3), name the link S999xxxxx where 999 is a unique number and xxxxx is the script name.
NOTE that scripts are executed in numerical order.
Also make a link in /sbin/rc2.d, nake this link K999xxxxx - this will stop the script when you move from run-level 3 down to run-level 2.

Check man ln for details of how to make the links.

HTH,

John
If you never make a mistake you'll never make anything.
andrea_53
Advisor

Re: how to start a script during the boot at run level 3

thanks a lots to everyone to help us. We still need some advice about:

1)
set_return()..
x=$?
.....
fi
what does x=$? mean?

2)
if [ -f /etc/rc.config.d/cron ]; then
. /etc/rc.config.d/cron
else....

what does . /etc/rc.config.d/cron mean?
Joseph C. Denman
Honored Contributor
Solution

Re: how to start a script during the boot at run level 3

Hi Andrea,

1)
set_return()..
x=$?
.....
fi
what does x=$? mean?
$? is the return value of the command.
0=completed successfully
1=had errors


2)
if [ -f /etc/rc.config.d/cron ]; then
. /etc/rc.config.d/cron
else....

what does . /etc/rc.config.d/cron mean?

It means get the variables from the file. Normally the rc.config.d directory contains files that have specific configuration variables for the rc script.

Hope this helps,

...jcd...
If I had only read the instructions first??
Frank Slootweg
Honored Contributor

Re: how to start a script during the boot at run level 3

1) $? is the RETURN VALUE (exit code) of the previous command.

2) ". /path/script" means that the script is 'sourced', i.e. it is read by the current shell instead of by a (shell) child of the current shell. It is done when things must be done in the current shell, like for example setting environment variables which have to be exported (because you can export to a child but not from a child to a parent).

A. Clay Stephenson
Acclaimed Contributor

Re: how to start a script during the boot at run level 3

Hi:

what does x=$? mean?

$? is the status of the last command; by convention, 0 usually indicates success and any other value indicates failure BUT while that may almost always be true you need to man the command to know for sure.


if [ -f /etc/rc.config.d/cron ]; then
. /etc/rc.config.d/cron
else....

The -f ... part tests whether the file /etc/rc/config.d/cron exists and if it is a regular file (as opposed to a directory, for example).

. /etc/rc.config.d/cron tells the shell to include /etc/rc.config.d/cron as part of the current shell. It is not started as a child process but rather is 'sourced' and becomes simply a part of the current shell. It is important that there is no unintended 'exit' statement in this 'sourced' file or you will exit both the 'sourced' file and the current shell since they are now one and the same.

If it ain't broke, I can fix that.
andrea_53
Advisor

Re: how to start a script during the boot at run level 3

We need the last information about the mean of:

if [ "$CRON" -eq 1 -a -x /usr/sbin/cron ];....

what does -a -x file mean?
Where can we usually find out this kind of informations?

Thanks very much from the happy italian guys
James R. Ferguson
Acclaimed Contributor

Re: how to start a script during the boot at run level 3

Hi:

if [ "$CRON" -eq 1 -a -x /usr/sbin/cron ];....

This expression tests the variable CRON for equality to one (1) AND ('-a') tests whether or not the file /usr/sbin/cron is executable ('-x').

Have a look at the man pages for 'test'.

Regards!

...JRF...
andrea_53
Advisor

Re: how to start a script during the boot at run level 3

thanks to everyone to have helped us.
Good luck