IBM Support

Starting and Stopping Software via System V RC Directories

Question & Answer


Question

How can I start up and stop my software on AIX?

Answer

This document describes how to start and stop software using run level directories via /etc/inittab. A run level is a software configuration that allows only a selected group of processes to exist.

Unix System V run level directories were introduced in AIX 4.3.3 Maintenance Level 05.

For another method to start or stop an application during a reboot or shutdown, refer to the document, Automated Startup and Shutdown of Custom Software.

The objective of run level script feature is to allow customers to start and stop selected applications by changing the run level. The directories are provided for customers to place their own stop and start scripts.


Background
During system startup, after the root file system has been mounted in the pre-initialization process, the init command is run as the last step of the startup process. The init command attempts to read the /etc/inittab file. If an "initdefault" entry exists, the init command uses the specified run level as the initial system run level. Run level 2 is defined by default to contain all of the terminal processes and daemons that are run in the multiuser environment. This can be seen in the /etc/inittab file:

 # lsitab init:


init:2:initdefault:

NOTE: Booting the system into mulitiuser mode using a runlevel other than the default, 2, is not supported by IBM. For a list of valid run levels, see man pages for init or telinit.

The framework to use this method of starting and stopping software consists of:

1. Nine directories, corresponding to the system run levels:

/etc/rc.d


/etc/rc.d/rc2.d
/etc/rc.d/rc3.d
/etc/rc.d/rc4.d
/etc/rc.d/rc5.d
/etc/rc.d/rc6.d
/etc/rc.d/rc7.d
/etc/rc.d/rc8.d
/etc/rc.d/rc9.d

2. Eight entries in your /etc/inittab

l2:2:wait:/etc/rc.d/rc 2


l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
l7:7:wait:/etc/rc.d/rc 7
l8:8:wait:/etc/rc.d/rc 8
l9:9:wait:/etc/rc.d/rc 9

The system will automatically run the start or kill scripts when entering a given run level, then proceed to run all or start scripts to start up the applications necessary at that level. In this manner, some applications could be stopped while others started when entering a run level.

When shutting down the system or rebooting using the /usr/sbin/shutdown command, the Kill scripts for every run level will be run. This ensures all custom applications are finished before fully shutting down AIX.


One special script exists, the /etc/rc.d/rc script - This script is designed to use the input run level to visit the appropriate /etc/rc.d/rc<runlevel>.d. It first executes scripts in this directory starting with K to stop the applications. Then, it executes scripts starting with S to start the applications.

Using Scripts to Stop or Start Applications

Sample scripts are provided in the /etc/rc.d/samples directory. See the Commands Reference for information about /etc/inittab, telinit, and init command.

  1. Create a shell script that includes the commands (provided by the application vendor) to stop or start that program. To use the scripts, the user must copy them to the appropriate /etc/rc.d/rc<run level>.d directory. The /etc/rc.d/rc shell script will only vist the directory structures under rc.d.

    NOTE: The script name must start with a K to kill (stop) or an S to start the application.
  2. Make the script executable by running the chmod command:
    # chmod +x myscript
  3. In both cases, it is advisable to use a file naming convention with a numeric after the K or S, and a short description of the process or service to be killed or started. The controlling RC script will run these in numeric order as it finds them. For example, a script to stop and start lpd daemon could be named K70lpd and S70lpd respectively.
  4. The run level can be changed by running:
    # telinit <run level>

    This tells the init command to place the system in one of the run levels. When the init command requests a change to run levels 0-9, it kills all processes at the current run levels and then restarts any processes associated with the new run levels.

    To check current run level, run who -r . It will return something similar to the following output:
    run level 2 Oct 4 14:23         2    0     S
    In this example, the system is running at the default run level 2.
  5. Kill scripts may need to be put into a different runlevel than the Start script.

    When AIX starts up and enters a runlevel, it will execute the Kill scripts it finds to stop services that should not be running in that runlevel.

    Then it will execute all the Start scripts for that runlevel.

    Customers have noticed that some applications do not handle killing it before it has even started. In order to avoid this, move the Kill script to an unused run level, such as runlevel 0, in the directory /etc/rc.d/rc0.d/

    During shutdown, AIX will execute ALL Kill scripts in ALL run levels, so the Kill script put into /etc/rc.d/rc0.d/ will be found and run.
  6. If a script is duplicated in more than one runlevel, it will be run for each runlevel that is entered.

    For example if a script is put into /etc/rc.d/rc2.d/ as well as /etc/rc.d/rc4.d/ upon entering runlevel 2 the script will be run, and if the system admin changes to runlevel 4 the script will also be run there.

    This is the same as the inittab entries, if they include multiple runlevels they will be run each time one of those runlevels is entered.

Related Information

[{"Product":{"code":"SWG10","label":"AIX"},"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Component":"File management","Platform":[{"code":"PF002","label":"AIX"}],"Version":"5.2;5.3;6.1;7.1","Edition":"","Line of Business":{"code":"","label":""}}]

Historical Number

isg1pTechnote1446

Document Information

Modified date:
17 June 2018

UID

isg3T1000661