preface

Many of the processes in the project are registered as services and run automatically when the system starts. Take time to carefully study systemd service management, here to make a summary.

Systemd overview

I have read a column on Geek Time called “Interesting Linux Operating System” and learned that the first process in the system is init, and all subsequent processes are started by init. Services are basically started with scripts, the main ones under /etc/init.d. There are two main types of services: standalone mode and hosted mode. In independent mode, the service is started independently and resident in memory. In managed mode, the socket and port are managed through Inetd or xinetd, and the corresponding service program is woken up when the user needs it.

Systemd basically replaces the old initd to manage services. It has the following characteristics:

  • Processing all services in parallel speeds up the boot process.initThe startup script is executed sequentially, which is slow.systemdYou can execute unrelated services concurrently and start faster.
  • throughsystemctlimplementationon-demandThe startup mode of. The defaultsystemdIs a resident memory process that executessystemctlCommand can quickly achieve service management.
  • Support for checking service dependencies.systemdDependency descriptions of related services are described through service files.systemdYou can parse service files to automatically manage service startup. Save the trouble of manual management.
  • supportdaemonClassification.systemdtheunitIt can be divided into many types,service, socket, target, path, timer,snapshotAnd so on.
  • Related services can be grouped, but there is onetargetA service of type contains multiple related services of other types for easy management.
  • Compatible withinitService script.

Systemd also has the disadvantage of not being able to implement a one-to-one correspondence with the init function. But it’s worth sacrificing some functionality for easier startup.

The configuration file

Systemd service files are located in three main places.

  • /lib/systemd/system/: indicates the installation location of the configuration file during software installation. The default location does not need to be changed.
  • /run/systemd/system/: service script generated during system execution. The content of the script in this directory can overwrite the content of the previous script.
  • The/etc/systemd/system / : this issystemdThe actual running script. A typical automated service would create a link here to the first two locations,systemdThe files in the current directory will be used to start services.

There are also several related directories:

  • /etc/sysconfig/*: stores some initial parameter configurations
  • /var/lib: Stores generated data files
  • /run/: Files generated during running, such as LOCK files and PID files. These files may also be stored under /run/

Systemd service unit classification:

  • The service. General service classification, mainly system service, process service. The most common type
  • Socket. Internal exchange, communication between the main process bar, specific need to be in-depth understanding, not very commonly used
  • The target. Group type that can perform a bunch of other services.
  • Mount. Mount related services to the file system.
  • The path. A service used to detect files in a specific directory.
  • The timer. A service that executes in a loop.

Service management

Systemd manages the service using the systemctl directive:

Systemctl start XXX Starts the service systemctl stop XXX stops the service systemctl restart XXX restarts the service systemctl reload XXX do not stop the service, reload the configuration, and make the configuration take effect. Systemctl enable XXX Enables automatic service startup. Systemctl disable XXX Delete the systemctl status XXX link in /etc/systemd/system to check the status of the systemctl service List-units --type=service --all Displays services of all service typesCopy the code

Services can be monitored by systemD only if they are managed by systemCTL.

There are a few tricks. For example, switch between graphical. Target and multi-user. Target:

Systemctl get-default // Get the current target systemctl set-default multi-user.target // Set the default mode systemctl ISOLATE for the next startup Graphical. Target // Switch to GRAPHICAL mode Linux also provides shortcut commands to switch between different modes [root@study ~]# systemctl poweroff System shutdown [root@study ~]# systemctl [root@study ~]# systemctl suspend enters pause mode [root@study ~]# systemctl Hibernate enters hibernation mode [root@study ~]# [root@study ~]# systemctl list-dependencies [unit] [--reverse] // View the services that a unit depends on, --reverse shows who the unit depends on (use)Copy the code

Service file writing

Here is the contents of the sshd.service file

Cat/usr/lib/systemd/system/SSHD. Service [Unit] # the project and the explanation of this Unit, performing service dependency on the Description = OpenSSH server daemon After=network.target SSHD -keygen.service Wants= SSHD -keygen.service [service] # EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -D $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] # WantedBy=multi-userCopy the code

Most of the service file formats are similar to these, but some of the related options are different.

Multiple service instances

Systemd also supports setting up multiple different instances of the same service, primarily through [email protected]. The @ symbol indicates that multiple instances of the service can be implemented, and then a configuration file with the parameter %I will be used to implement different instances of the service.

Original file: execute service name. service Executable file: Execute service name. example name. serviceCopy the code

The more common ones are VTTY, as well as services in different cyberspace under Ubuntu.

Systemd is a simple principle, a lot of details of a service manager. If there is a need to use it, it needs to be thoroughly understood. For general use, understanding the above commands and knowledge is basically enough.

Learning systemd this knowledge, relatively easy to read or bird brother Linux private house dishes, although there are some implementation of a little bit different, there is a bay bay translation with our habits or a little bit different. Reading books is good, but the best material is English documents. Another is ruan Yifeng’s blog, the quality of the teacher’s blog is great.

In addition, Systemd is an open source program, you can find the source code on Github, systemCTL implementation with a DBUS program related, next time talk about DBUS service, mainly in the project needs to use DBUS service to achieve a function, I also have a simple understanding.