控制服务和守护进程systemd

systemd简介

systemd是用户空间的第一个应用程序,即/sbin/init

SysV⻛格特点:

Upstart风格特点:

Systemd风格特点:

系统启动和服务器进程由systemd系统和服务管理器进行管理。此程序提供了一种方式,可以在启动时和运行中的系统上激活系统资源、服务器守护进程和其他进程。

守护进程是在执行各种任务的后台等待或运行的进程。为了侦听连接,守护进程使用套接字。套接字可以由守护进程创建,或者与守护进程分离,并且可能由另一个进程创建(如systemd),随后在客户端建立连接时将套接字传递到守护进程。

服务通常指一个或多个守护程序,但启动或停止服务可能会一次性更改系统的状态(如配置网络接口),并且不会使守护程序继续运行。

[En]

A service usually refers to one or more daemons, but starting or stopping a service may make an one-time change to the state of the system (such as configuring a network interface) and does not leave the daemon to continue to run.

在系统启动时实现服务并行启动

[En]

Realize service parallel startup when the system is booted

按需激活进程
系统状态快照
基于依赖关系定义业务控制逻辑

[En]

Define service control logic based on dependency relationship

systemd使用unit的概念来管理服务,这些unit表现为一个个配置文件。

systemd通过对这些配置文件进行标识和配置达到管理服务的目的:

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

Unit的类型:

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关键特性:

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

不兼容特性:

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

使用systemctl管理服务

//语法:systemctl COMMAND name[.service|.target]
//常用COMMAND:
    start name.service      //启动服务
    stop name.service       //停止服务
    restart name.service    //重启服务
    status name.service     //查看服务状态
    try-restart name.service            //条件式重启服务,若服务已经启动则重启,若服务未启动则不做任何操作
    reload-or-restart name.service      //重载或重启服务,能reload则reload,否则restart
    reload-or-try-restart name.service  //重载或条件式重启服务,能reload则reload,否则try-restart
    mask name.service       //禁止设定为开机自启
    unmask name.service     //取消禁止设定为开机自启
    list-dependencies name.service      //查看服务的依赖关系
    is-active name.service      //查看某服务当前激活与否的状态
    is-enabled name.service      //查看服务是否开机自动启动
    enable name.service     //设定某服务开机自动启动
    disable name.service    //禁止服务开机自动启动
    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    //混合睡眠,快照并挂起
//语法:systemctl COMMAND name[.service|.target]
//常用COMMAND:
    start name.service      //启动服务
    stop name.service       //停止服务
    restart name.service    //重启服务
    status name.service     //查看服务状态
    try-restart name.service            //条件式重启服务,若服务已经启动则重启,若服务未启动则不做任何操作
    reload-or-restart name.service      //重载或重启服务,能reload则reload,否则restart
    reload-or-try-restart name.service  //重载或条件式重启服务,能reload则reload,否则try-restart
    mask name.service       //禁止设定为开机自启
    unmask name.service     //取消禁止设定为开机自启
    list-dependencies name.service      //查看服务的依赖关系
    is-active name.service      //查看某服务当前激活与否的状态
    is-enabled name.service      //查看服务是否开机自动启动
    enable name.service     //设定某服务开机自动启动
    disable name.service    //禁止服务开机自动启动
    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    //混合睡眠,快照并挂起

启动服务、停止服务、重新启动服务、检查服务状态

[En]

Start the service, stop the service, restart the service, check the service status

[root@lnh system]# systemctl start httpd.service  //开启
[root@lnh system]# systemctl status httpd.service //查看状态
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor >
   Active: active (running) since Tue 2022-07-12 23:15:02 CST; 31min ago
 Main PID: 36390 (httpd)
    Tasks: 82 (limit: 11205)
   Memory: 24.5M
   CGroup: /system.slice/httpd.service
           ├─36390 /usr/local/src/httpd/bin/httpd
           ├─36391 /usr/local/src/httpd/bin/httpd
           ├─36392 /usr/local/src/httpd/bin/httpd
           └─36393 /usr/local/src/httpd/bin/httpd

Jul 12 23:15:02 lnh systemd[1]: Starting httpd server daemon...

Jul 12 23:15:02 lnh httpd[36389]: AH00558: httpd: Could not reliably determ>
Jul 12 23:15:02 lnh systemd[1]: Started httpd server daemon.

[root@lnh system]# systemctl stop httpd.service     //关闭
[root@lnh system]# systemctl status httpd.service  //查看状态
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor >
   Active: failed (Result: exit-code) since Tue 2022-07-12 23:46:40 CST; 2s>
  Process: 36682 ExecStop=/usr/local/src/httpd/bin/httpd -s stop (code=exit>
 Main PID: 36390 (code=exited, status=0/SUCCESS)

Jul 12 23:46:40 lnh httpd[36682]:   -S                 : a synonym for -t ->
Jul 12 23:46:40 lnh httpd[36682]:   -t -D DUMP_MODULES : show all loaded mo>
Jul 12 23:46:40 lnh httpd[36682]:   -M                 : a synonym for -t ->
Jul 12 23:46:40 lnh httpd[36682]:   -t -D DUMP_INCLUDES: show all included >
Jul 12 23:46:40 lnh httpd[36682]:   -t                 : run syntax check f>
Jul 12 23:46:40 lnh httpd[36682]:   -T                 : start without Docu>
Jul 12 23:46:40 lnh httpd[36682]:   -X                 : debug mode (only o>
Jul 12 23:46:40 lnh systemd[1]: httpd.service: Control process exited, code>
Jul 12 23:46:40 lnh systemd[1]: httpd.service: Failed with result 'exit-cod>
Jul 12 23:46:40 lnh systemd[1]: Stopped httpd server daemon.

[root@lnh system]# systemctl restart httpd.service //重启
[root@lnh system]# systemctl status httpd.service  //查看状态
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor >
   Active: active (running) since Tue 2022-07-12 23:47:11 CST; 2s ago
  Process: 36682 ExecStop=/usr/local/src/httpd/bin/httpd -s stop (code=exit>
  Process: 36692 ExecStart=/usr/local/src/httpd/bin/httpd (code=exited, sta>
 Main PID: 36693 (httpd)
    Tasks: 82 (limit: 11205)
   Memory: 34.7M
   CGroup: /system.slice/httpd.service
           ├─36693 /usr/local/src/httpd/bin/httpd
           ├─36694 /usr/local/src/httpd/bin/httpd
           ├─36695 /usr/local/src/httpd/bin/httpd
           └─36696 /usr/local/src/httpd/bin/httpd

Jul 12 23:47:11 lnh systemd[1]: Starting httpd server daemon...

Jul 12 23:47:11 lnh httpd[36692]: AH00558: httpd: Could not reliably determ>
Jul 12 23:47:11 lnh systemd[1]: Started httpd server daemon.

有条件地重新启动服务,如果服务已启动则重新启动,如果服务未启动则不执行任何操作

[En]

Restart the service conditionally, restart if the service has been started, and do nothing if the service is not started

[root@lnh system]# systemctl status httpd.service
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor >
   Active: failed (Result: exit-code) since Tue 2022-07-12 23:46:40 CST; 2s>
  Process: 36682 ExecStop=/usr/local/src/httpd/bin/httpd -s stop (code=exit>
 Main PID: 36390 (code=exited, status=0/SUCCESS)

Jul 12 23:46:40 lnh httpd[36682]:   -S                 : a synonym for -t ->
Jul 12 23:46:40 lnh httpd[36682]:   -t -D DUMP_MODULES : show all loaded mo>
Jul 12 23:46:40 lnh httpd[36682]:   -M                 : a synonym for -t ->
Jul 12 23:46:40 lnh httpd[36682]:   -t -D DUMP_INCLUDES: show all included >
Jul 12 23:46:40 lnh httpd[36682]:   -t                 : run syntax check f>
Jul 12 23:46:40 lnh httpd[36682]:   -T                 : start without Docu>
Jul 12 23:46:40 lnh httpd[36682]:   -X                 : debug mode (only o>
Jul 12 23:46:40 lnh systemd[1]: httpd.service: Control process exited, code>
Jul 12 23:46:40 lnh systemd[1]: httpd.service: Failed with result 'exit-cod>
Jul 12 23:46:40 lnh systemd[1]: Stopped httpd server daemon.

[root@lnh system]# systemctl restart httpd.service
[root@lnh system]# systemctl status httpd.service
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor >
   Active: active (running) since Tue 2022-07-12 23:47:11 CST; 2s ago
  Process: 36682 ExecStop=/usr/local/src/httpd/bin/httpd -s stop (code=exit>
  Process: 36692 ExecStart=/usr/local/src/httpd/bin/httpd (code=exited, sta>
 Main PID: 36693 (httpd)
    Tasks: 82 (limit: 11205)
   Memory: 34.7M
   CGroup: /system.slice/httpd.service
           ├─36693 /usr/local/src/httpd/bin/httpd
           ├─36694 /usr/local/src/httpd/bin/httpd
           ├─36695 /usr/local/src/httpd/bin/httpd
           └─36696 /usr/local/src/httpd/bin/httpd

Jul 12 23:47:11 lnh systemd[1]: Starting httpd server daemon...

Jul 12 23:47:11 lnh httpd[36692]: AH00558: httpd: Could not reliably determ>
Jul 12 23:47:11 lnh systemd[1]: Started httpd server daemon.

[root@lnh system]# systemctl try-restart httpd.service
[root@lnh system]# systemctl status httpd.service
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor >
   Active: active (running) since Tue 2022-07-12 23:51:02 CST; 8s ago
  Process: 36793 ExecStop=/usr/local/src/httpd/bin/httpd -s stop (code=exit>
  Process: 36795 ExecStart=/usr/local/src/httpd/bin/httpd (code=exited, sta>
 Main PID: 36797 (httpd)
    Tasks: 82 (limit: 11205)
   Memory: 26.5M
   CGroup: /system.slice/httpd.service
           ├─36797 /usr/local/src/httpd/bin/httpd
           ├─36798 /usr/local/src/httpd/bin/httpd
           ├─36799 /usr/local/src/httpd/bin/httpd
           └─36800 /usr/local/src/httpd/bin/httpd

Jul 12 23:51:02 lnh systemd[1]: Starting httpd server daemon...

Jul 12 23:51:02 lnh httpd[36795]: AH00558: httpd: Could not reliably determ>
Jul 12 23:51:02 lnh systemd[1]: Started httpd server daemon.

[root@lnh system]# systemctl status httpd.service
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor >
   Active: active (running) since Tue 2022-07-12 23:51:02 CST; 15s ago
  Process: 36793 ExecStop=/usr/local/src/httpd/bin/httpd -s stop (code=exit>
  Process: 36795 ExecStart=/usr/local/src/httpd/bin/httpd (code=exited, sta>
 Main PID: 36797 (httpd)
    Tasks: 82 (limit: 11205)
   Memory: 26.3M
   CGroup: /system.slice/httpd.service
           ├─36797 /usr/local/src/httpd/bin/httpd
           ├─36798 /usr/local/src/httpd/bin/httpd
           ├─36799 /usr/local/src/httpd/bin/httpd
           └─36800 /usr/local/src/httpd/bin/httpd

Jul 12 23:51:02 lnh systemd[1]: Starting httpd server daemon...

Jul 12 23:51:02 lnh httpd[36795]: AH00558: httpd: Could not reliably determ>
Jul 12 23:51:02 lnh systemd[1]: Started httpd server daemon.

禁止设定为开机自启,取消禁止设定为开机自启,开机自启,取消开机自启

[root@lnh system]# systemctl mask httpd.service
Created symlink /etc/systemd/system/httpd.service → /dev/null.

[root@lnh system]# systemctl enable --now httpd.service
Failed to enable unit: Unit file /etc/systemd/system/httpd.service is masked.

[root@lnh system]# systemctl unmask httpd.service
Removed /etc/systemd/system/httpd.service.

[root@lnh system]# systemctl enable --now httpd.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

[root@lnh system]# systemctl disable httpd.service
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.

CentOS6以前版本的系统控制服务方式

service NAME start      //启动NAME服务
service NAME stop       //停止NAME服务
service NAME status     //查看NAME服务状态
service NAME restart    //重启NAME服务
chkconfig --add NAME    //把NAME脚本纳入管理并设置开机自启
chkconfig NAME on       //设置开机自启
chkconfig NAME off      //设置开机不自动启动

Original: https://www.cnblogs.com/tushanbu/p/16476772.html
Author: 涂山布
Title: 控制服务和守护进程systemd

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523106/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球