1847014 Members
3837 Online
110258 Solutions
New Discussion

Re: shutdown & script

 
SOLVED
Go to solution
Philip J. Priest_1
Frequent Advisor

shutdown & script

I have a shell script that trims and archives log files in /var/adm. If the system is rebooted or shutdown is there a way i can run my script at shutdown time?
14 REPLIES 14
harry d brown jr
Honored Contributor

Re: shutdown & script

Better to do it at BOOT time, because usually you want your shutdown script to be quick! Of course we want our reboot to be fast also.

Just put your script in /sbin/init.d

then put an entry in (symbolic link) in /sbin/rc3.d

Make sure your script's symbolic link is a HIGH S### number, like S992cleanup.rc.

for shutdown look in rc1.d, and create a sym-link for a K### file name


live free or die
harry
Live Free or Die
James R. Ferguson
Acclaimed Contributor

Re: shutdown & script

Hi Philip:

For information on the startup/shutdown mechanisms, see:

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

This ia worth reading. It describes the necessary, but simple, tasks you need to perform to create startup and shutdown scripts.

Regards!

...JRF...

Philip J. Priest_1
Frequent Advisor

Re: shutdown & script

So i can have my shell script in /sbin/init.d ? i dont have to do anything else other than link it in /sbin/rc3.d?
Jeff Schussele
Honored Contributor

Re: shutdown & script

Hi Phillip,

You may want to consider creating an config file in /etc/rc.config.d to easily toggle this function on & off.
You'd source this file at the top of the start/stop script in /sbin/init.d

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Sajid_1
Honored Contributor

Re: shutdown & script

hello,

Yes, place the script in /sbin/init.d, link it to the link files (start and Kill) in rc?.d (depend on run level). If you are looking for another configuration file through which you can pass arguments to this script, then place it in the /etc/rc.config.d/ directory.
If you are only looking for the shutdown time, then you only need a link file in the rc directory starting with K???script_name (kill script at shutdown time).

HTH
learn unix ..
Roger Baptiste
Honored Contributor

Re: shutdown & script

<>

Yes, it's that simple. In your case, since you want to excute it only at shutdown time (and not startup time), you need to link it to a "K" script in the /sbin/rc2.d directory.
(Note: It will go in rc2.d and NOT rc3.d, since the normal init default level is 3 and shutdown scripts will be always one level below the default).
Simply put; if your script is /sbin/init.d/Log_trim, then the link would be
/sbin/rc2.d/K1000log_trim . (1000 is an example , it can be any unused number ).

HTH
raj
Take it easy.
Philip J. Priest_1
Frequent Advisor

Re: shutdown & script

Raj you sure it goes into /sbin/rc2.d not /sbin/rc1.d? when you do a shutdown -ry now what syncer directory is run rc1 or rc2?
James R. Ferguson
Acclaimed Contributor
Solution

Re: shutdown & script

Philip:

The document I suggested you read will answer and clarify your questions.

I???ll summarize the key points here.

/usr/share/doc/start_up.txt

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

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.

In general, user applications would be started at runlevel-3 (i.e. /sbin/rc3.d/) and killed in runlevel-2 (i.e. /sbin/rc2.d).

Regards!

...JRF...
Roger Baptiste
Honored Contributor

Re: shutdown & script

hi,

You can run it under rc2.d or rc1.d . It depends on what your script is trimming. For instance, if you are trimming syslog.log files, it would make sense to run it after syslogd daemon stops and that corresponds to
/sbin/rc1.d/K780syslogd, so your script would be having a lower K number than 780 and will be in rc1.d directory.

HTh
raj
Take it easy.
James R. Ferguson
Acclaimed Contributor

Re: shutdown & script

Hi (again) Philip:

Rajman's choice of '/sbin/rc1.d/' for your kill script is very appropriate in this case for the reason he cited -- viz. that the 'syslogd' is killed at this level. You want to execute your script *after* the 'syslogd' daemon terminates. If 'syslogd' is killed at "K780" then an appropriate link for your script might be "K781".

Regards!

...JRF...

Philip J. Priest_1
Frequent Advisor

Re: shutdown & script

ok now i got it =)
but is /usr mounted at this point? cos my script uses gzip.
James R. Ferguson
Acclaimed Contributor

Re: shutdown & script

Hi Philip:

'/usr' is still mounted at level-1. If you look at '/sbin/rc0.d/' you will see the unmount of filesystems in '/sbin/init.d/localmount'.

Regards!

...JRF...
Allan Pincus_1
New Member

Re: shutdown & script

Phillip,

Perhaps you have already considered this, but why not just set up a simple cron job that runs the script morning, noon, or night?

Just a thought.

- Allan
Philip J. Priest_1
Frequent Advisor

Re: shutdown & script

Allan i do have a cron thats running this once a month, what i need to have is what if the system is rebooted or brought down? I still wanna archive my logs.