Control services and daemons .md

Control services and daemons

1.systemd

1.1.systemd Profile

systemd is the first user application space, i.e., / sbin / init

type init program :
SysV style : When init (centos5), for system initialization, subsequent initialization operations are achieved by means of the script
features:
a large number of command script contains, for each command must start a process, executing the command after going to stop the process. As a result, a large number of system initialization process is created, the destruction process, the efficiency is very low.
There may be dependencies between services, must strictly follow a certain order to start the service, a service did not finish before starting the back of the service can not be executed during startup. Not parallel.
Configuration file: / etc / inittab
the Upstart Style : init (centos6), developed by the ubuntu, close to the bus in the form of work in a parallel manner, efficient than Sysv.
Features:
based on bus ways to allow inter-process an application communicate with each other
without waiting for service startup is complete, as long as an initialization can put their state returns to other processes
configuration file: / etc / inittab, / etc / init / * conf.
systemd style : systemd (centos7)
features: start faster than SysV and Upstar almost
do not need to start the service by any scripts, systemd itself can start the service, which itself is a powerful interpreter, no sh / bash startup services participation
systemd is not true when the system is initialized to start any service
as long as the service is not used, it tells you started, did not actually start. Only when the first will really start to access services
Profile: / usr / lib / systemd / system, / etc / systemd / system

System startup and server processes managed by systemd system and service manager. This program provides a way to activate the system resources at startup and operation of the system on the server daemons and other processes.
A daemon is a process in the background waiting to perform various tasks or running. To listen to the connection daemon socket. A socket may be created by a daemon, or the daemon separated, and may be created by another process (e.g. systemd), then the client establishes a socket connection is transmitted to the daemon.
Service generally refers to one or more daemon processes, but to start or stop a service status might be a one-time changes to the system (such as configuring network interfaces), continue to run after without leaving a daemon.

New features of 1.2.systemd

Achieve service system boot and start
-demand activation process
system state snapshot
dependency-based service control logic definitions

1.3.systemd core concepts Unit

systemd unit uses the concept of managed services, which is a unit performance profiles.
By systemd purpose of identification and configuration of these configuration files to achieve management services:

//这些unit文件中主要包含了系统服务、监听socket、保存的系统快照
//及其它与init相关的信息保存至以下目录:
    /usr/lib/systemd/system
    /run/systemd/system
    /etc/systemd/system

Unit Type :

Service unit    //文件扩展名为.service,用于定义系统服务
Target unit     //文件扩展名为.target,用于模拟实现“运行级别”
    runlevel0.target和poweroff.target        //关机
    runlevel1.target和rescue.target          //单用户模式
    runlevel2.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel3.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel4.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel5.target和graphical.target       //图形级别
    runlevel6.target和reboot.target          //重启
Device unit     //文件扩展名为.device,用于定义内核识别的设备
Mount unit      //文件扩展名为.mount,用于定义文件系统挂载点
Socket unit     //文件扩展名为.socket,用于标识进程间通信用的socket文件
Snapshot unit   //文件扩展名为.snapshot,用于管理系统快照
Swap unit       //文件扩展名为.swap,用于标识swap设备
Automount unit  //文件扩展名为.automount,用于实现文件系统的自动挂载点
Path unit       //文件扩展名为.path,用于定义文件系统中的一个文件或目录

Unit Key Features

//基于socket的激活机制:
    socket与服务程序分离,当有人去访问时才会真正启动服务,以此来实现按需激活进程与服务的并行启动
//基于bus的激活机制:
    所有使用dbus实现进程间通信的服务,可以在第一次被访问时按需激活
//基于device的激活机制:
    支持基于device激活的系统服务,可以在特定类型的硬件接入到系统中时,按需激活其所需要用到的服务
//基于path的激活机制:
    某个文件路径变得可用,或里面出现新文件时就激活某服务
//系统快照:
    保存各unit的当前状态信息于持久存储设备中,必要时能自动载入
//向后兼容sysv init脚本

Incompatible characteristics

//systemctl命令固定不变
//非由systemd启动的服务,systemctl无法与之通信
//只有已经启动的服务在级别切换时才会执行stop,在centos6以前是所有S开头的服务全部start,所有K开头的服务全部stop
//系统服务不会读取任何来自标准输入的数据流
//每个服务的unit操作均受5分钟超时时间限制

2. Use systemctl Management Services

//语法:systemctl COMMAND name[.service|.target]
//常用COMMAND:
    start name.service      //启动服务
    stop name.service       //停止服务
    restart name.service    //重启服务
    status name.service     //查看服务状态
[root@localhost ~]# systemctl stop postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2019-09-25 15:11:46 CST; 23s ago
  Process: 2146 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 1177 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 1165 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 1097 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 1321 (code=killed, signal=TERM)

9月 25 10:06:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 10:06:43 localhost.localdomain postfix/postfix-script[1319]: starting the Postfix mail system
9月 25 10:06:43 localhost.localdomain postfix/master[1321]: daemon started -- version 2.10.1, configuration ...fix
9月 25 10:06:43 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:11:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:11:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl start postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:12:51 CST; 9s ago
  Process: 2146 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 2168 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 2165 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 2163 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2240 (master)
   CGroup: /system.slice/postfix.service
           ├─2240 /usr/libexec/postfix/master -w
           ├─2241 pickup -l -t unix -u
           └─2242 qmgr -l -t unix -u

9月 25 15:12:51 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:12:51 localhost.localdomain postfix/master[2240]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:12:51 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl restart postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:14:20 CST; 16s ago
  Process: 2250 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 2264 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 2262 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 2259 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2336 (master)
   CGroup: /system.slice/postfix.service
           ├─2336 /usr/libexec/postfix/master -w
           ├─2337 pickup -l -t unix -u
           └─2338 qmgr -l -t unix -u

9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
 try-restart name.service            //条件式重启服务,若服务已经启动则重启,若服务未启动则不做任何操作
[root@localhost ~]# systemctl stop postfix.service
[root@localhost ~]# systemctl try-restart postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 42s ago
  Process: 2346 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 2264 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 2262 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 2259 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2336 (code=killed, signal=TERM)

9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
reload-or-restart name.service      //重载或重启服务,能reload则reload,否则restart
    reload-or-try-restart name.service  //重载或条件式重启服务,能reload则reload,否则try-restart
mask name.service       //禁止设定为开机自启
    unmask name.service     //取消禁止设定为开机自启
[root@localhost ~]# systemctl mask postfix.service
Created symlink from /etc/systemd/system/postfix.service to /dev/null.
[root@localhost ~]# systemctl status postfix.service
● postfix.service
   Loaded: masked (/dev/null; bad)
   Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 3min 11s ago
 Main PID: 2336 (code=killed, signal=TERM)

9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl unmask postfix.service
Removed symlink /etc/systemd/system/postfix.service.
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 3min 27s ago
 Main PID: 2336 (code=killed, signal=TERM)

9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
list-dependencies name.service      //查看服务的依赖关系
    is-active name.service      //查看某服务当前激活与否的状态
    is-enable name.service      //查看服务是否开机自动启动
[root@localhost ~]# systemctl list-dependencies postfix.service
postfix.service
● ├─system.slice
● └─basic.target
● ├─microcode.service
● ├─rhel-autorelabel-mark.service
● ├─rhel-autorelabel.service
● ├─rhel-configure.service
● ├─rhel-dmesg.service
● ├─rhel-loadmodules.service
● ├─[email protected]
● ├─paths.target
● ├─slices.target
● │ ├─-.slice
● │ └─system.slice
● ├─sockets.target
● │ ├─dbus.socket
● │ ├─dm-event.socket
● │ ├─systemd-initctl.socket
● │ ├─systemd-journald.socket
● │ ├─systemd-shutdownd.socket
● │ ├─systemd-udevd-control.socket
● │ └─systemd-udevd-kernel.socket
● ├─sysinit.target
● │ ├─dev-hugepages.mount
● │ ├─dev-mqueue.mount
● │ ├─kmod-static-nodes.service
● │ ├─lvm2-lvmetad.socket
● │ ├─lvm2-lvmpolld.socket
● │ ├─lvm2-monitor.service
● │ ├─plymouth-read-write.service
lines 2-30
[root@localhost ~]# systemctl is-active postfix.service
active
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:22:42 CST; 4min 22s ago
  Process: 2413 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 2411 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 2408 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2485 (master)
   CGroup: /system.slice/postfix.service
           ├─2485 /usr/libexec/postfix/master -w
           ├─2486 pickup -l -t unix -u
           └─2487 qmgr -l -t unix -u

9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
enable name.service     //设定某服务开机自动启动
    disable name.service    //禁止服务开机自动启动
[root@localhost ~]# systemctl enable postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:22:42 CST; 8min ago
 Main PID: 2485 (master)
   CGroup: /system.slice/postfix.service
           ├─2485 /usr/libexec/postfix/master -w
           ├─2486 pickup -l -t unix -u
           └─2487 qmgr -l -t unix -u

9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl disable postfix.service
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:22:42 CST; 10min ago
 Main PID: 2485 (master)
   CGroup: /system.slice/postfix.service
           ├─2485 /usr/libexec/postfix/master -w
           ├─2486 pickup -l -t unix -u
           └─2487 qmgr -l -t unix -u

9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
 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     //查看所有的级别
[root@localhost ~]# systemctl list-unit-files --type service
UNIT FILE STATE   
arp-ethers.service disabled
auditd.service enabled 
[email protected] enabled 
blk-availability.service disabled
brandbot.service static  
[email protected] static  
chrony-wait.service disabled
chronyd.service enabled 
console-getty.service disabled
console-shell.service disabled
[email protected] static  
cpupower.service disabled
crond.service enabled 
dbus-org.fedoraproject.FirewallD1.service enabled 
dbus-org.freedesktop.hostname1.service static  
dbus-org.freedesktop.import1.service static  
dbus-org.freedesktop.locale1.service static  
dbus-org.freedesktop.login1.service static  
dbus-org.freedesktop.machine1.service static  
dbus-org.freedesktop.NetworkManager.service enabled 
dbus-org.freedesktop.nm-dispatcher.service enabled 
dbus-org.freedesktop.timedate1.service static  
dbus.service static  
debug-shell.service disabled
dm-event.service disabled
dracut-cmdline.service static  
dracut-initqueue.service static  
dracut-mount.service static  
get-default     //查看默认运行级别
    set-default name.target     //设置默认运行级别
[root@localhost ~]# systemctl get-default
multi-user.target
[root@localhost ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
    rescue      //切换至紧急救援模式(大多数服务不启动,但是会加载驱动)
    emergency   //切换至emergency模式(驱动不会加载,系统不会初始化,服务不会启动)
    halt        //关机
    poweroff    //关机
    reboot      //重启
    suspend     //挂起系统,此时不能关机,否则无用
    hibernate   //创建并保存系统快照,下次系统重启时会自动载入快照
    hybrid-sleep    //混合睡眠,快照并挂起

Guess you like

Origin www.cnblogs.com/liping0826/p/11585105.html