You get a bonus - 1 coin for daily activity. Now you have 1 coin

25 - Manage Demons linux

Lecture



Today we will talk about Linux processes that are called demons , and also see how daemons (or services are managed, if we draw a parallel with Windows ). The main difference between the daemon process and the usual user process is that the daemon does not have a controlling terminal, and the user can interact with it only through other programs or control scripts. So, in the simplest case, a daemon is an executable file or script that runs in the background. Very often such files end with the letter d (from daemon ): sshd, httpd, cupsd - although this is not necessary.

In order to manage the daemon, there is a control script for each daemon. Such scripts are located, as a rule, in the /etc/init.d/ directory. Such scripts are named as the demon itself (only without the letter d at the end), although this is not an unshakable rule. For example, the /etc/init.d/ssh script controls the sshd daemon, which is located in the / usr / sbin / directory.

What is meant by the management of the demon? This is the ability to perform certain operations, such as starting a daemon, stopping, restarting, forced stopping and restarting, and some others. Therefore, when launching the control script, we must transfer to it a parameter that describes the action performed with the daemon. These parameters are strictly defined, and the main and most common are:

start - start the daemon
stop - stop the daemon
restart - restart daemon
reload - reload (re-read configuration files) daemon parameters
force-reload - force reload of daemon parameters

If you yourself write such a control script, you must remember that it must handle at least two parameters: start and stop . You can open any script from the /etc/init.d/ directory and see how the handling of control parameters is implemented through the case construct.

So let's try to stop and start the cron daemon . To do this, the /etc/init.d/cron control script is located in the /etc/init.d/ directory:

one
2
3
four
five
6
7
eight
9

debian: / home / igor # /etc/init.d/cron
Usage: /etc/init.d/cron {start | stop | restart | reload | force-reload}.
debian: / home / igor # /etc/init.d/cron stop
Stopping periodic command scheduler: crond.
debian: / home / igor # ps ax | grep [c] ron
debian: / home / igor # /etc/init.d/cron start
Starting periodic command scheduler: crond.
debian: / home / igor # ps ax | grep [c] ron
6524? Ss 0:00 / usr / sbin / cron

If you run it without parameters (line 1), you will see a hint of what parameters you need to pass to this script (line 2). We try to start with the stop parameter (line 3) and check that the daemon is stopped (line 5). Then we start the daemon (line 6) and check (line 8). In the same way there is a management of other demons.

Let's now see how the daemons start when the Linux operating system boots and when it stops. As you should remember in Linux there is such a thing as runlevel - the level of system startup. At each level of system startup, a clearly defined number of daemons are executed. When moving from level to level, demons that should not work — are completed, and which should work — are started. In order to tell the system which daemons at which startup level they should start or stop in different distributions, there are specially designed utilities for this. But we will now look at the mechanism itself of the system for launching demons in order to understand its essence.

In the / etc directory there are directories named rcN.d , where N is the symbol indicating the runlevel to which the directory belongs. That is, we have the following directories: rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d, rc6.d and rcS.d. If you look at the contents of the directories, you can see that they contain symbolic links to scripts from the /etc/init.d/ directory:

one
2
3
four
five
6
7
eight
9
ten
eleven
12

debian: / home / igor # ls -l /etc/rc6.d/
lrwxrwxrwx 1 root root 13 2008-09-05 17:20 K01gdm -> ../init.d/gdm
lrwxrwxrwx 1 root root 17 2009-02-23 16:09 K09apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2008-09-05 16:48 K11anacron -> ../init.d/anacron
lrwxrwxrwx 1 root root 13 2008-09-05 17:08 K11atd -> ../init.d/atd
lrwxrwxrwx 1 root root 14 2008-09-05 14:44 K11cron -> ../init.d/cron
lrwxrwxrwx 1 root root 15 2008-09-05 17:18 K19hplip -> ../init.d/hplip
...
...
lrwxrwxrwx 1 root root 18 2008-09-05 14:43 S40umountfs -> ../init.d/umountfs
lrwxrwxrwx 1 root root 20 2008-09-05 14:43 S60umountroot -> ../init.d/umountroot
lrwxrwxrwx 1 root root 16 2008-09-05 14:43 S90reboot -> ../init.d/reboot

Symbolic links are named according to the following rule: first comes the Latin capital letter S or K , then a two-digit number and then the exact name of the script to which the symbolic link refers. The letter K in the name of the link means that the script to which it refers should be executed with the stop parameter. That is, K11cron (line 6) means that the *** will be executed *** and /etc/init.d/cron stop . That is, the cron daemon will be stopped. Accordingly, the letter S means that the script pointed to by the link should be executed with the start parameter. A two-digit number determines the order in which the scripts are executed and, accordingly, the order in which the daemons are started or terminated. The first to run scripts with smaller numbers. Thus, the resolution of script dependencies (daemons) is implemented. For example, the cron daemon should be stopped only after the apache2 daemon has been stopped (lines 6 and 3). If the symbolic links have the same number, this means that the demons are independent of each other and the scripts can be executed in any order. It should also be noted that first all the scripts with the letter K are executed, and then only all the scripts with the letter S.

As you know, all user processes (and demons also belong to those) start with the init process, and which sequentially reads the / etc / inittab file . Among others, the following lines are in / etc / inittab :

one
2
3
four
five
6
7

l0: 0: wait: /etc/init.d/rc 0
l1: 1: wait: /etc/init.d/rc 1
l2: 2: wait: /etc/init.d/rc 2
l3: 3: wait: /etc/init.d/rc 3
l4: 4: wait: /etc/init.d/rc 4
l5: 5: wait: /etc/init.d/rc 5
l6: 6: wait: /etc/init.d/rc 6

When the system goes to any runlevel , for example, to the sixth, the script /etc/init.d/rc is executed, to which the number of the launch level is passed as a parameter - 6. As a result of its work, the script /etc/init.d/rc starts execute, in accordance with the rules described above, all scripts for which there are symbolic links in the /etc/rc6.d/ directory. If simplified, then the name of each symbolic link is converted from the form K01gdm to /etc/init.d/gdm stop , and S10sysklogd to /etc/init.d/sysklogd start .

Thus, if you want a daemon to start (or stop) at the level you want, you need to create the corresponding symbolic link in the appropriate /etc/rcN.d/ directory. For example, if you do not want the cron daemon to run at all levels, it means that you must remove the link of the form S80cron from all the /etc/rcN.d/ directories.

If you have your own daemon (for example, mydaemon with the mydaemon control script) and you want to run it at the 5th launch level, then you need to create a symbolic link in the /etc/rc5.d/ directory:

one

igor @ adm-ubuntu: ~ / linux $ sudo ln -s /etc/init.d/mydaemon /etc/rc5.d/S99mydaemon

The control script should of course be located in the /etc/init.d/ directory.

By removing and adding symbolic links to the /etc/rcN.d/ directories, you can configure the start and stop of daemons at the appropriate launch levels.

In many distributions there are utilities that allow you to speed up the process of managing the launch of daemons for different runlevels . For Rad Hat distributions, this is the chkconfig utility, for debian distributions it is update-rc.d or sysv-rc-conf . There are also similar programs with a graphical interface, for example, KSysV . For utilities like chkconfig in control scripts, it is also customary to write information with default parameters. This information is set as comments at the very beginning of the script. Here is an example:

one
2
3
four
five
6
7
eight

### BEGIN INIT INFO
# Provides: apache2
# Required-Start: $ local_fs $ remote_fs $ network $ syslog
# Required-Stop: $ local_fs $ remote_fs $ network $ syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start / stop apache2 web server
### END INIT INFO

If you execute com *** in chkconfig without additional parameters, then symbolic links will be created with the letter S in the /etc/rcN.d directories for launch levels - 2 3 4 5, and with the letter K in the /etc/rcN.d directories for start levels 0 1 6.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

LINUX operating system

Terms: LINUX operating system