Automatic Startup and Shutdown on UNIX Systems

Shutdown and startup for UNIX systems varies with platform, but there are generally two varieties: BSD style systems use one file, /etc/inittab, which specifies what scripts to run in each run level, while System V flavors of UNIX use scripts which reside in a set of directories called rcn.d, where n is an number ranging from 0 on up. Usually these subdirectories are in the /etc directory, but can be in others (HP-UX 10 has them under /sbin, for example). Here, we're going to give examples of how to configure each of the systems we support.

No matter which UNIX platform you're using, if you want to have the PI API automatically start up when the system boots or reboots, and shut down when the system shuts down, you MUST ensure that the .profile or .login file for the PI API user does not require interaction with a terminal. This means that if you use, for example, tset to set the TERM variable, you must first check to see if there is a terminal attached to the current process. One way to do this is:

if tty -s ; then tset .... fi

A given interface may have specific startup requirements, for example, it may need to be started after the SCADA or DCS system is running. For details about the startup requirements of your interface, refer to the interface documentation.

Automatic Startup/Shutdown for Solaris 2.x

Solaris recognizes numerous run levels:

Run Level Purpose
s or S single user (used for administering the system)
2 standard level, non-networked
3 default level
4 standard level, user-defined

Which processes run at each level is determined by a set of scripts, /etc/rcS through /etc/rc6. These scripts look in the directories /etc/rc#.d (where # = 0, 2, 3, etc.) for scripts that begin with a K or an S. The K scripts are used to kill processes, and the S scripts are used to start processes. When the system moves from level 3 to level 2, the K scripts in /etc/rc2.d are executed first, then the S scripts. The scripts are run in ASCII collated sequence, so K02stop is run before K04metoo. On shutdown or reboot, the system runs the scripts in /etc/rc0.d. If you are using the default run level, you will need to put the PI API startup in /etc/rc3.d. The shutdown script for PI API needs to be in /etc/rc2.d, so the PI API will be shut down if the system goes to non-networked mode, and also in /etc/rc0.d, so the PI API will be shut down if the system is halted or rebooted.

Note: Solaris systems should be restarted using shutdown -i 6. This will cause the shutdown scripts to be run before running reboot. The reboot command simply restarts the kernel, without taking the time to shut down processes properly. The command shutdown -i 5 is for powering down.

Script files are actually kept in /etc/init.d, with links placed in the /etc/rc#.d directories. So, in /etc/init.d, you need to create the following files: PIstart and PIstop. Be sure to change the lines that specify where to find the PI API on your system.

In the example that follows, the PI API user is piadmin. If your PI API user is different, substitute your PI API username wherever piadmin appears.

#!/bin/ksh

#

# Filename: /etc/init.d/PI

#

# become piadmin to start/stop PI-API

PATH=/sbin:/usr/sbin:/usr/bin

export PATH

case "$1" in

'start')

if [ -f /etc/init.d/PIstart ] ; then

su - piadmin -c "/etc/init.d/PIstart" > /dev/console 2>&1

fi

;;

'stop')

if [ -f /etc/init.d/PIstop ] ; then

su - piadmin -c "/etc/init.d/PIstop" > /dev/console 2>&1

fi

;;

*)

echo "usage: $0 {start|stop}"

;;

esac

#!/bin/ksh

#

# Filename: /etc/init.d/PIstart

# Make sure to set the directory in these

# files to your PIHOME directory, in all

# places

#

if [ -f /opt/piapi/bin/pistart ] ; then

cd /opt/piapi/bin

nohup ksh pistart

fi

#!/bin/ksh

#

# Filename: /etc/init.d/PIstop

# Make sure to set the directory in these

# files to your PIHOME directory, in all

# places

#

if [ -f /opt/piapi/bin/pistop ] ; then

cd /opt/piapi/bin

ksh pistop

fi

Next, set the owner, group, and permissions on these files to match the other files in this directory:

root> chgrp sys /etc/init.d/PI*

root> chmod 755 /etc/init.d/PI*

Then, you'll need to set the links in the rc#.d directories. Make sure that the S## number on the startup file is higher than the S## number for inetd, and that the K## number for the kill file is lower than the K## number for inetd (in both directories).

root> ln -s /etc/init.d/PI /etc/rc3.d/S96PI

root> ln -s /etc/init.d/PI /etc/rc2.d/K04PI

root> ln -s /etc/init.d/PI /etc/rc0.d/K04PI

Verify that these files have the same owner, group, and permissions as other startup files in those directories.

Finally, test your scripts before you restart your machine. To stop the PI API:

root> sh /etc/rc0.d/K04PI stop

Verify that the PI API processes shut down.

root> sh /etc/rc3.d/S96PI start

Verify that the PI API starts properly.

If there is any problem with stopping or restarting the PI API, remove the links in the /etc/rc#.d directories until you've debugged and fixed the problems. The files in the /etc/init.d directory will not affect your system as long as there are not links in the /etc/rc#.d directories.

Automatic Startup/Shutdown for HP-UX 10

HP-UX 10 recognizes numerous run levels:

Run Level Purpose
s or S single user (used for administering the system)
1 – 6 standard levels (1 is single-user, 2 is default, 3 and up are user-defined)

Which processes run at each level is determined by the /sbin/rc script. This script looks in the directories /sbin/rc#.d (where # = 1, 2, 3, etc.) for scripts that begin with a "K" or an "S". The "K" scripts are used to kill processes, and the "S" scripts are used to start processes. When moving down from a higher level to a lower level, all "K" scripts in all the intervening levels are run. When moving up to a higher level, all "S" scripts in all intervening levels are run. So when the system moves from level 4 to level 2, the "K" scripts in /sbin/rc3.d are executed, then the "K" scripts in /sbin/rc2.d. The scripts are run in ASCII collated sequence, so K002stop is run before K004metoo.

If you are using the default run level of 2, you must put the PI API startup in /sbin/rc3.d. The shutdown script for the PI API needs to be in /sbin/rc%.d, where % is the default run level, minus 1, so the PI API will be shut down if the system goes out of the default run level to any other level, or if the system is halted or rebooted. To determine your default run level, check the line in /etc/inittab that reads "init:#:initdefault:." The # is the default run level for the system.

Script files are actually kept in /sbin/init.d, with links placed in the /sbin/rc#.d directories. So, in /sbin/init.d, you need to create the following files: PIstart and PIstop. Be sure to change the lines that specify where to find the PI API on your system. In addition, if you are not using piadmin as your PI API user, substitute your PI API username wherever piadmin appears.

#!/bin/ksh
#
# Filename: /sbin/init.d/PI
#
# become piadmin to start/stop PI-API
PATH=/sbin:/usr/sbin:/usr/bin
export PATH
case "$1" in
'start')
if [ -f /sbin/init.d/PIstart ] ; then
su - piadmin -c "/sbin/init.d/PIstart" > /dev/console 2>&1
fi
;;
'stop')
if [ -f /sbin/init.d/PIstop ] ; then
su - piadmin -c "/sbin/init.d/PIstop" > /dev/console 2>&1
fi
;;
'start_msg')
echo "Starting PI"
;;
'stop_msg')
echo "Shutting down PI, please wait"
;;
*)
echo "usage: $0 {start|stop}"
;;
esac


#!/bin/ksh
#
# Filename: /sbin/init.d/PIstart
# Make sure to set the directory in these
# files to your PIHOME directory, in all
# places
#
if [ -f /opt/piapi/bin/pistart ] ; then
cd /opt/piapi/bin
nohup ksh pistart
fi


#!/bin/ksh
#
# Filename: /sbin/init.d/PIstop
# Make sure to set the directory in these
# files to your PIHOME directory, in all
# places
#
if [ -f /opt/piapi/bin/pistop ] ; then
cd /opt/piapi/bin
ksh pistop
fi
   

Next, set the owner, group, and permissions on these files to match the other files in this directory:

root> chgrp sys /sbin/init.d/PI*

root> chmod 755 /sbin/init.d/PI*

Then, you'll need to set the links in the rc#.d directories. Make sure the S### number on the startup file is higher than the S### number for inetd, and the K### number for the kill file is lower than the K### number for inetd (in both directories). Note that HP-UX uses three digits in the link file names as opposed to the two digits used by other varieties of UNIX.

Here, run level 3 is our default run level, so we're putting the start script in /sbin/rc3.d, and the kill script in /sbin/rc2.d:

root> ln -s /sbin/init.d/PI /sbin/rc3.d/S960PI
root> ln -s /sbin/init.d/PI /sbin/rc2.d/K004PI
   

Verify that these files have the same owner, group, and permissions as other startup files in those directories.

Finally, test your scripts before you restart your machine. To stop the PI API:

root> sh /sbin/rc2.d/K004PI stop
   

Verify that the PI API processes shut down.

root> sh /sbin/rc3.d/S960PI start
   

Verify that the PI API starts properly.

If there is any problem with stopping or restarting the PI API, remove the links in the /sbin/rc#.d directories until you've debugged and fixed the problems. The files in the /sbin/init.d directory will not affect your system as long as there are no links in the /sbin/rc#.d directories.

Automatic Startup/Shutdown for IBM AIX

IBM AIX recognizes numerous run levels:

Run Level Purpose
s (S) or m (M) Single user (used for administering the system)
2 default multi-user run level
3 - 9 user defined levels

The system determines what processes should run at each level by reading the /etc/inittab file. The lines in this file tell the system what scripts to run at what run levels. The line with the initdefault in it (init:#:initdefault:, where # is some number 2-9) tells the system the default run level. Each line with this number after the first colon is executed when entering the default run level. Thus, we need to add a line to the /etc/inittab file (again, substitute your PI API username for piadmin below):

pisystem:2:once:su - piadmin -c /etc/rc.pistart > /dev/console 2>&1 # Start PI

Caution: Before editing /etc/inittab, you must save the original under another name. If you do not and make an error while editing, your system will not boot.

If your initdefault level is not 2, you should replace the 2 with the appropriate number from initdefault. For shutdown and reboot, the system uses the /etc/rc.shutdown script. If this file does not exist, you should create it. Otherwise, just add the last line below to your current file. If you have to create the /etc/rc.shutdown file, you should give it the same owner, group, and permissions as the /etc/rc file. As before, you are strongly advised to save a copy of the original file under another name before editing this file.

#! /bin/ksh
#
# Filename: /etc/rc.shutdown
#
su - piadmin -c "/etc/rc.pistop" > /dev/console 2>&1
   

Now you'll have to create these two files you've told the system to use, /etc/rc.pistart and /etc/rc.pistop. Be sure to change the directory in these files to the PI Server directory on your system.

#!/bin/ksh
#
# Filename: /etc/rc.pistart
#
if [ -f opt/piapi/bin/pistart ] ; then
cd /opt/piapi/bin
nohup ksh pistart
fi
   

#!/bin/ksh
#
# Filename: /etc/rc.pistop
#
if [ -f /opt/piapi/bin/pistop ] ; then
cd /opt/piapi/bin
ksh pistop
fi
   

You'll need to change the permissions on these files so that piadmin (or your PI API user) can run them:

root> chmod 755 /etc/rc.pi*

Finally, test your scripts before you restart your machine. To stop the PI API (replace piadmin with your PI API user if different):

root> su - piadmin -c "/etc/rc.pistop" > /dev/console 2>&1

Verify that the PI API processes shut down (replace piadmin with your PI API user if different).

root> su - piadmin -c /etc/rc.pistart > /dev/console 2>&1

Verify that the PI API starts properly.

If there is any problem with stopping or restarting the PI API, you'll need to restore the previous versions of /etc/inittab and /etc/rc.shutdown until you can find and fix the problem.

Automatic Startup/Shutdown for Red Hat Enterprise Linux 4/5/6

It is important to note that the following procedures are applicable to Red Hat Enterprise Linux 4/5/6 distributions. For all other distributions, we advise you to double check the manuals for the appropriate definitions for the run levels and locations of the start and stop scripts as well as the best practices and good conventions.

Red Hat recognizes numerous run levels:

Run Level Purpose
0 Run when system is being shut down.
1 Single user mode – typically used on administration.
2 User defined, typically unused.
3 Multi-user mode.
4 Not used / Reserved.
5 Full multi-user mode with GUI.
6 Reboot.

Which processes run at each level is determined by a set of scripts typically located in /etc/init.d. The various run levels, /etc/rc0 through /etc/rc6 look in the directories /etc/rc#.d (where # = 0, 1, 2, 3, etc.) for symlinks to scripts in /etc/init.d. These symlinks begin with a K or an S. The K scripts are used to kill processes, and the S scripts are used to start processes. When the system moves from level 3 to level 2, the K scripts in /etc/rc2.d are executed first, then the S scripts. The scripts are run in ASCII collated sequence, so K02stop is run before K04metoo. On shutdown the system runs the scripts in /etc/rc0.d. You will need to put the PI API startup and shutdown scripts in /etc/init.d.

Script files are actually kept in /etc/init.d, with symlinks placed in the /etc/rc#.d directories. So, in /etc/init.d, you need to create the following files: PIstart and PIstop. Be sure to change the lines that specify where to find the PI API on your system, and use your PI API user in place of piadmin if applicable.

#!/bin/ksh
#
# Filename: /etc/init.d/PIAPI
#
# become piadmin to start/stop PI-API 
PATH=/sbin:/usr/sbin:/usr/bin 
export PATH 
case "$1" in 
'start') 
if [ -f /etc/init.d/PIstart ] ; then 
su - piadmin -c "/etc/init.d/PIstart" > /dev/console 2>&1 
fi 
;; 
'stop') 
if [ -f /etc/init.d/PIstop ] ; then 
su - piadmin -c "/etc/init.d/PIstop" > /dev/console 2>&1 
fi 
;; 
*) 
echo "usage: $0 {start|stop}" 
;; 
esac 
  
#!/bin/ksh 
# 
# Filename: /etc/init.d/PIstart 
# Make sure to set the directory in these 
# files to your PIHOME directory, in all 
# places 
# 
if [ -f /opt/piapi/bin/pistart ] ; then 
cd /opt/piapi/bin 
nohup ksh pistart 
fi 
  
#!/bin/ksh 
# 
# Filename: /etc/init.d/PIstop 
# Make sure to set the directory in these 
# files to your PIHOME directory, in all 
# places 
# 
if [ -f /opt/piapi/bin/pistop ] ; then 
cd /opt/piapi/bin 
ksh pistop 
fi

Next, set the owner, group, and permissions on these files to match the other files in this directory:

root> chgrp sys /etc/init.d/PI*
root> chmod 755 /etc/init.d/PI*

Then, you'll need to set the links in the rc#.d directories. Make sure that the S## number on the startup file is higher than the S## number for inetd, and that the K## number for the kill file is lower than the K## number for inetd (in both directories).

root> ln -s /etc/init.d/PIAPI /etc/rc3.d/S99PI
root> ln -s /etc/init.d/PIAPI /etc/rc5.d/S99PI
root> ln -s /etc/init.d/PIAPI /etc/rc6.d/K04PI
root> ln -s /etc/init.d/PIAPI /etc/rc0.d/K04PI

Verify that these files have the same owner, group, and permissions as other startup files in those directories.

Finally, test your scripts before you restart your machine. To stop PI:

root> sh /etc/rc0.d/K04PI stop

Verify that the PI API processes shut down.

root> sh /etc/rc3.d/S99PI start

Verify that the PI API starts properly.

If there is any problem with stopping or restarting the PI API, remove the links in the /etc/rc#.d directories until you've debugged and fixed the problems. The files in the /etc/init.d directory will not affect your system as long as there are not links in the /etc/rc#.d directories.

 

Enabling Operational Intelligence