Introduction
In the last "Linux Startup Journey", we learned about the Linux Startup Process. In the last step of the process, the INIT process pulls/etc/init. d/rcN. d/daemon ). If a custom process also needs to be started along with the system, how can we set it?
Process hosting using init
InitProgramAccording to the configuration in the/etc/inittab file, pull the system service and Getty login terminal, and the custom process can also be managed by the INIT program.
The simplest method is to add a statement to the/etc/inittab file:
ZZ:2345: Respawn:/tmp/LX/test_init
The statement indicates:
ID: Unique Identifier of the entry in the inittab file. ZZ is used here.
Runlevels: List the running levels of a specified command or process. The instructions for running the/tmp/LX/test_init program are 2, 3, 4, and 5.
Action: Describes how init manages a process. respawn indicates that the process is restarted as long as it is aborted.
Process: Process to be executed. Here it is/tmp/LX/test_init.
After modifying the/etc/inittab file, we can send the HUP semaphore to the init process for the modification to take effect:
# Kill-HUP1
After the preceding command is executed, you can find our custom program:
Ser PID % CPU % mem vsz RSS tty stat Start Time Command
Root37830.00.03832600? SS mar270:00/Tmp/LX/test_init
Can init manage custom processes as we set them? Kill the above process and try again!
This method is very simple to set the process to start and execute for a long time. All you need to do is prepare the program and add a statement in the/etc/inittab file. However, to stop the process, we need to modify the/etc/inittab file and add a comment at the beginning of the corresponding entry, then, send the sighup signal to re-load the/etc/inittab configuration file of the INIT process.
At this time, the aunt glanced at the screen and inadvertently said: It is so inconvenient to stop the process. What if I want to restart the process, query the Process status, and modify the process running level?
Daemon settings
To conveniently start, stop, and query a custom process, we can set a custom process as a daemon process and use services and other tools to manage the process. The following describes how to set the daemon.
First, like the built-in daemon. the d directory must have an init script corresponding to the custom daemon. The script contains the following content.
Description (init info ):
#/Etc/init. d/test_daemon
### Begin init info
# Provides: test_daemon
# Required-start: $ local_fs
# Shocould-start:
# Required-stop:
# Shocould-stop:
# Default-start:2 3 5
# Default-stop:0 1 2 6
# Description: test_daemon writes a Message to/tmp/LX/test. log every10Seconds,
# Showing that the daemon is alive.
### End init info
The description of the daemon is in a fixed format. It indicates the services that the daemon depends on and the running level of startup/shutdown.
Script for starting, stopping, and status query:
Test_bin =/usr/sbin/test_daemon
Case "$1" In
Start)
Echo-n"Starting test_daemon"
/Sbin/startproc $ test_bin
Rc_status-V
;;
Stop)
Echo-n"Shutting down test_daemon"
/Sbin/killproc-term $ test_bin
Rc_status-V
;;
The above script calls the startproc and killproc tools to control the start and stop of the daemon.
Finally, set the script permission correctly and use the insserv tool to generate the corresponding link in the/etc/init. d/rcN. d directory:
# Chmod744/Etc/init. d/test_daemon
# Insserv/etc/init. d/test_daemon
The insserv tool determines the relative startup sequence between a custom daemon and an existing daemon Based on the dependency information of the "init Info" in the daemon init script:
# Ll/etc/init. d/rc5.d/*Test
Lrwxrwxrwx 1 Root 7 Mar 29 0:02/etc/init. d/rc5.d/k01test_daemon-> ../test_daemon
Lrwxrwxrwx 1 Root 7 Mar 29 0:02/etc/init. d/rc5.d/s01test_daemon-> ../test_daemon
After completing the above steps, the process test_daemon will automatically start after the system restarts. We can use the command to start and stop the process:
#/Etc/init. d/test_daemon start
Starting test_daemon done
#/Etc/init. d/test_daemon status
CheckingForService test_daemon running
#/Etc/init. d/test_daemon stop
Shutting down test_daemon done
The service tool can manage system services under the/etc/init. d directory, or use it to perform the same operations as the above:
# Service test_daemon start
# Service test_daemon status
# Service test_daemon stop
In addition, we can use the chkconfig tool to query and set the startup level of test_daemon:
# Chkconfig -- list | grep test_daemon
Test_daemon0: Off1: Off2: On3: On4: Off5: On6: Off
# Chkconfig test_daemon 35
# Chkconfig -- list | grep Test
Test0: Off1: Off2: Off3: On4: Off5: On6: Off
Summary
This article introduces two methods to add custom background processes. The process is managed by init. When the process exits, it can be pulled up again by init. The setting method is simple, but it is not convenient to manage the process. The process is set as a system service item by using init script, the existing service item management tools service and chkconfig can be used to conveniently manage processes.
The custom process used in this articleCodeAnd Related scripts can be downloaded from here.
---
This article is based on suse11sp1 (x86_64) and can be downloaded from this release.
# Cat/etc/Suse-release; uname-R
SuSE Linux Enterprise Desktop11(X86_64)
Version =11
Patchlevel =1
2.6.32.12-0.7-Default
Reference: Automatic startup of dsmcad daemon on system reboot on SuSE Linux
Creating a persistent daemon with init. Hack 4 in chapter 1. Server basics, Linux Server hacks
Document/etc/init. d/readme under suse11sp1
Document/etc/init. d/skeleton under suse11sp1
Linux daemon writing howto