Question & Answer
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.
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 multi-user environment. This can be seen in the /etc/inittab file:
# lsitab init:
NOTE: Booting the system into mulitiuser mode using a run level 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:
2. Eight entries in your /etc/inittab
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<run level>.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.
- 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 visit 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.
- Make the script executable by running the chmod command:
# chmod +x myscript
- 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.
Scripts in a given directory are run in the order that they are listed by 'ls'. For example S99app1 is executed before S9app1. This matters if one script depends on a service started by another script.
- 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.
- Kill scripts may need to be put into a different run level than the Start script.
When AIX starts up and enters a run level, it will execute the Kill scripts it finds to stop services that should not be running in that run level.
Then it will execute all the Start scripts for that run level.
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 run level 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.
- If a script is duplicated in more than one run level, it will be run for each run level 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 run level 2 the script will be run, and if the system admin changes to run level 4 the script will also be run there.
This is the same as the inittab entries, if they include multiple run levels they will be run each time one of those run levels is entered.
Was this topic helpful?
19 December 2021