Linux Chapter 7-Control Services and Daemons

Introduction to Linux

One, systemd

1.1 Introduction to systemd

  • It is the first process started by the system and also the first application in user space, namely /sbin/init (in redhat5/6 is the init program)
  • Type of init program: (automatically delayed start service.msc)
  • (Script method) 1. (Serial startup, slow) SysV style: init (centos5), when the system is initialized, the subsequent initialization operations are implemented with the help of scripts

Features:
-The script contains a large number of commands, each command must start a process, and the process must be terminated after the command is executed. In this way, a large number of processes will be created and destroyed when the system is initialized, and the work efficiency will be very low
-there may be dependencies between services, and services must be started in strict accordance with a certain order. Unable to perform the startup process. Not parallel
configuration files: / etc / inittab
-

  • (Script method) 2. (Bus-based startup, slightly faster) Upstart style: init (centos6), developed by ubuntu, works in a manner close to parallel through a bus, and is more efficient than SysV

Feature:
An application based on the bus mode that allows processes to communicate with each other. It does
not need to wait for the service to start up, and can return its status to other processes as long as it is initialized.
Configuration files: /etc/inittab, /etc/init/*.conf

  • (Start systemd) 3. (Access start service, systemd first start, fast) Systemd style: systemd (centos7)

    Features: Startup speed is faster than SysV and Upstart.
    No script is needed to start the service. Systemd can start the service itself. It is a powerful interpreter. When starting the service, no sh/bash participation is required.
    Systemd is not really in the system. Start any service during initialization
    As long as the service is not used, it tells you that it is started, but it is not actually started. Only when the first visit will actually start the service

  • Configuration files: /usr/lib/systemd/system, /etc/systemd/system

  • System startup and server processes are managed by the systemd system and service manager. This program provides a way to activate system resources, server daemons, and other processes on startup and running systems.

  • A daemon is a process that waits or runs in the background while performing various tasks. In order to listen for connections, the daemon uses sockets to "communicate" (ip and port to control). The socket can be created by the daemon process, or separated from the daemon process, and may be created by another process (such as systemd), and then the socket is passed to the daemon process when the client establishes a connection.

  • "Service usually refers to one or more daemons", but starting or stopping a service may make a one-time change to the state of the system (such as configuring network interfaces) without leaving the daemon to continue running.

1.2, new features of systemd

  • a, to achieve parallel start of services when the system boots
  • b, activate the process on demand
  • c, system state snapshot
  • d, define service control logic based on dependencies

1.3, the core concept of systemd

Systemd uses the concept of unit (unit) to manage services. These units are represented as configuration files.
systemd achieves the purpose of managing services by identifying and configuring these configuration files:
//These unit files mainly include system services, monitoring sockets, saved system snapshots
// and other init-related information are saved to the following directory:
/ usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system

  • Unit type (redhat7/8 version) :
  • Service unit
Service unit //The file extension is .service, which is used to define system services
Target unit //The file extension is .target, which is used to simulate "run level"
runlevel0.target and poweroff.target //Shut down
runlevel1.target and rescue.target //Single user mode
runlevel2.target and multi-user.target //For systemd, there is no difference between 2/3/4 levels
runlevel3.target and multi-user.target //For systemd, there is no difference between 2/3/4 levels
runlevel4.target and multi-user.target //For systemd, there is no difference between 2/3/4 levels
runlevel5.target and graphical.target //Graphic level
runlevel6.target and reboot.target //Restart
Device unit //The file extension is .device, which is used to define the device recognized by the kernel
Mount unit //The file extension is .mount, which is used to define the file system mount point
Socket unit //The file extension is .socket, which is used to identify the socket file used for inter-process communication
Snapshot unit //The file extension is .snapshot, used to manage system snapshots
Swap unit //The file extension is .swap, which is used to identify the swap device
Automount unit //The file extension is .automount, which is used to realize the automatic mount point of the file system
Path unit //The file extension is .path, which is used to define a file or directory in the file system*

Insert picture description here

  • Unit type (redhat5/6 version) :

0 Shut down
1 Emergency mode/Single user mode
2, Multi-user mode
3, Multi-user mode
4, Multi-user mode
5, Graphical interface
6, Restart
init 0 Close
init 6 Restart
reboot

  • Unit key features
//Socket-based activation mechanism: The socket is separated from the service program, and the service will only be started when someone visits, so as to realize the parallel start of the on-demand activation process and the service
//Bus-based activation mechanism: All services that use dbus to achieve inter-process communication can be activated on demand when they are accessed for the first time
//Device-based activation mechanism: Support system services based on device activation. When a specific type of hardware is connected to the system, the services needed to be activated on demand
//Path-based activation mechanism: A certain file path becomes available, or a service is activated when a new file appears in it (the service is activated when the path file exists.)
//System snapshot: Save the current state information of each unit in a persistent storage device, which can be automatically loaded when necessary
//Backward compatible with sysv init script
  • Incompatible features

//systemctl command is fixed
//services that are not started by systemd, systemctl cannot communicate with them
//stop will be executed only when the services that have been started are switched in level, before centos6, all services starting with S are all started, all All services starting with K stop
//System services will not read any data stream from standard input
//Unit operations of each service are subject to a 5-minute timeout limit

Second, use systemctl to manage services

1. Management in systemctl redhat8

  • 语法:systemctl COMMAND name [service| .target]
  • Commonly used services:
start name.service //Start the service
stop name.service //Out of service
restart name.service //Restart the service
status name.service //View service status
try-restart name.service //Conditionally restart the service, if the service is already started, restart, if the service is not started, do nothing
reload-or-restart name.service //Reload or restart the service, reload if you can reload, otherwise restart
reload-or-try-restart name.service //Reload or conditionally restart the service, reload if possible, otherwise try-restart
mask name.service //It is forbidden to set to self-start after boot
unmask name.service //Cancel the prohibition of setting as self-startup
list-dependencies name.service //View service dependencies
is-active name.service //Check whether a service is currently activated or not
is-enabled name.service //Check whether the service starts automatically after booting
enable name.service //Set a service to start automatically
disable name.service //Forbid the service to start automatically
isolate name.target //切换至某级别,如systemctl isolate graphical.target就是切换至图形界
list-unit-files --type service //查看所有服务的开机自动启动状态(是否开机自启)
list-units --type service //查看所有已经激活的服务状态信息
list-units --type target //查看所有已装载的级别
list-units --type service --all //查看所有服务(已启动/已停止)的状态信息
list-units --type target --all //查看所有的级别
get-default //查看默认运行级别
set-default name.target //设置默认运行级别
rescue //切换至紧急救援模式(大多数服务不启动,但是会加载驱动)
emergency //切换至emergency模式(驱动不会加载,系统不会初始化,服务不会启动)
halt //关机 poweroff
reboot //重启
suspend //挂起系统,此时不能关机,否则无用
hibernate //创建并保存系统快照,下次系统重启时会自动载入快照
hybrid-sleep //混合睡眠,快照并挂起

2,Redhat7/8版本和Redhat5/6版本的基本应用对比

  • Redhat 6.5

    S10 network 通过s表示启动服务
    K90 network 通过k表示关闭服务

    • 启动服务的方式:

      Service network start
      Service network stop
      Service network restart
      Service network reload

    • 设置开机自动启动

      Chkconfig --add nerwork
      Chkconfig network on
      Chkconfig network off
      Check whether the program is loaded into the boot-up automatically
      [root@RHCE ~]# chkconfig --list |grep vsftpd
      vsftpd 0: off 1: off 2: enabled 3: enabled 4: enabled 5: enable 6: disable

  • Redhat6.5 system service configuration file
    Insert picture description here

  • The path of the configuration file and the path of the startup program.

Insert picture description here

  • Redhat 7/8

    Only the started service level will execute stop when switching

  • How to start the service:

    Systemctl start network
    Systemctl stop network
    Systemctl reload network
    Systemctl restart network

  • Set automatic startup

    Systemctl enable network
    Systemctl disable network
    Check whether the program is loaded into the boot-up automatically
    [root@Eryuege ~]# systemctl enable vsftpd
    Created symlink /etc/systemd/system/multi-user.target.wants/vsftpd.service → /usr/lib /systemd/system/vsftpd.service.
    [root@Eryuege ~]# systemctl list-dependencies |grep vsftpd
    ● ├─vsftpd.service

  • Redhat7/8 system service configuration files: /usr/lib/systemd/system, /etc/systemd/system

Insert picture description here

  • Start the service program in the way of file path, and delete the file path program after turning off the boot
  • The service is started when the running path exists when it is turned on, and the corresponding service is not started when the path does not exist.

Insert picture description here

Guess you like

Origin blog.csdn.net/LBJ19224/article/details/109218574