systemctl的操作详解总结及其与service的区别

参考ubuntu的官方文档
https://manpages.ubuntu.com/manpages/jammy/en/man5/systemd.unit.5.html
https://manpages.ubuntu.com/manpages/jammy/en/man5/systemd.service.5.html

Redhat linux系列的centos操作系统和Debian linux系列的ubuntu操作系统均如此

总结:
1、systemctl就是集service和chkconfig一体的功能,systemctl enable就是类似chkconfig,只要把文本类型的可执行文件放入/usr/lib/systemd/system/就能实现systemctl的操作,执行systemctl enable XX就是把XX加入开机启动,即通过/usr/lib/systemd/system/XX创建软连接/etc/systemd/system/multi-user.target.wants/XX
2、centos7之前的版本(不包括7),系统服务启动停止以及状态查看的管理命令工具是service,启动的第一个进程为init,init进程负责后面所有进程的启动,但是通过init进程启动的后续进程都是串行启动的,启动速度会慢一些(服务依靠shell脚本顺序启动)。而chkconfig命令用于设置某个服务在系统某个运行级别是否自启动,chkconfig可以理解为是一个用于维护/etc/rc[0-6].d目录(小知识点此目录下以S开头的命令是start的意思,K开头的命令是kill的意思)的命令行工具。service命令管理的服务其相关脚本存储目录:/etc/init.d
centos7以后版本用systemd服务统一管理服务进程,对于服务的启停以及开机自动启动由命令systemctl统一管理,第一个进程名为为systemd,systemd采用并行启动后续的进程而不是串行顺序,因此启动速度会比service更快一些,service属于SysVinit(System V Init),systemctl属于systemd,是SysVinit的继任者,systemd借鉴了MAC OS系统的launchd思想。systemctl采用了target的模式,centos7的/etc/systemd/system/multi-user.target.wants命令行模式为multi-user.target可理解为centos6中runlevel 3的模式,centos7的/etc/systemd/system/graphical.target.wants命令行模式为graphical.target可理解为centos6中runlevel 5模式。如centos6需在命令行模式下开机自启动的服务,则需要将对应服务脚本关联至runlevel 3启动加载的目录/etc/rc3.d下;而对应centos7中则是将对应服务脚本关联至对应的target目录/etc/systemd/system/multi-user.target.wants下。systemctl命令管理的服务其相关脚本存储目录:/usr/lib/systemd/system
3、/usr/lib/systemd/system目录下的文件不能使二进制的可执行文件,比如influxd为例子/usr/lib/systemd/system/influxd不能是二进制的可执行文件,只能是一个sh这样的文本的可执行文件,直接把二进制文件influxd加入/etc/init.d/无法正常service influxd start启动,加入/usr/lib/systemd/system/也无法正常systemctl start influxd启动,且influxdb.service服务里面不能直接写成ExecStart=/usr/bin/influxd &,systemctl start influxdb会报错Error: unknown command “&” for “influxd”;也不能写成ExecStart=/usr/bin/influxd,systemctl start influxdb会不停的自动重启,因为服务启动时间过长,达到超时时间就自动结束了服务并不停的自动重启然后出现报错influxdb.service start operation timed out. Terminating。因为influxd没有/usr/bin/influxd --help |grep daemonize或/usr/bin/influxd --help |grep quite这种后台运行的参数,所以还是需要&让它在后台跑,但是&又无直接ExecStart=/usr/bin/influxd &,所以需要借助“/bin/bash -c”将一个长字符串当作一条完整的命令来执行,即写成ExecStart=/bin/bash -c “/usr/bin/influxd &”
4、/lib/systemd/system和/usr/lib/systemd/system的区别,/lib目录是软链接名称,/lib实际指向/usr/lib。
/etc/systemd/system和/usr/lib/systemd/system的区别,/etc/systemd/system下面的文件都是软链接名称,这些软链接实际指向/lib/systemd/system目录的对应文件,/etc/systemd/system这个目录有点像以前centos 6的/etc/rc.d/rc5.d/之类的功能,执行优先序比/usr/lib/systemd/system高
/etc/systemd/system/multi-user.target.wants和/usr/lib/systemd/system的区别,/etc/systemd/system/multi-user.target.wants下面的文件都是软链接名称,这些软链接实际指向/usr/lib/systemd/system目录的对应文件
/lib/systemd/system和/lib/systemd/user的区别,系统system和用户user的区别
5、如果想让ExecStart的服务遇到不正常的退出信号时能够自动重启,则配置Restart=on-failure,如果想让ExecStart的服务只要出现退出就自动重启(除systemctl stop XX之外的其他所有退出动作比如主动kill掉后也能自动重启),则配置Restart=always

启动服务:systemctl start xxx.service
关闭服务:systemctl stop xxx.service
重启服务:systemctl restart xxx.service
显示服务的状态:systemctl status xxx.service
在开机时启用服务:systemctl enable xxx.service
在开机时禁用服务:systemctl disable xxx.service
查看服务是否开机启动:systemctl is-enabled xxx.service
查看服务有哪些依赖项:systemctl list-dependencies xxx.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed
重新加载一个服务的配置文件:systemctl reload xxx.service
重载所有修改过的配置文件:systemctl daemon-reload
查看systemctl服务相关日志:journalctl -xe

设置开机自启的命令即创建软连接文件将服务的管理脚本文件从服务目录/usr/lib/systemd/system/下,创建软连接至对应的target目录/etc/systemd/system/multi-user.target.wants下;关闭开机自启则是将对应服务脚本的链接文件从/etc/systemd/system/multi-user.target.wants/下删除。

[root@FRSBachDEV2 ~]# systemctl enable influxdb
Created symlink from /etc/systemd/system/influxd.service to /usr/lib/systemd/system/influxdb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/influxdb.service to /usr/lib/systemd/system/influxdb.service.
[root@FRSBachDEV2 ~]# ll /etc/systemd/system/multi-user.target.wants |grep influxdb |grep -v grep
lrwxrwxrwx. 1 root root 40 Feb 23  2022 influxdb.service -> /usr/lib/systemd/system/influxdb.service
[root@FRSBachDEV2 ~]# systemctl disable influxdb
Removed symlink /etc/systemd/system/multi-user.target.wants/influxdb.service.
Removed symlink /etc/systemd/system/influxd.service.
[root@FRSBachDEV2 ~]# ll /etc/systemd/system/multi-user.target.wants |grep influxdb |grep -v grep

systemctl的启动文件存放在/usr/lib/systemd/system目录下,文件的模块主要分为Unit,Unit类型,Install这3个模块
1、[unit]
Unit本身的说明,以及与其他相依daemon的设置,包括在什么服务之后才启动此unit之类的设置值,Unit常用的选项:
可选项 描述
Description 对当前服务的简单描述
After 可以指定在哪些服务之后进行启动
Before 可以指定在哪些服务之前进行启动
Requires 指定依赖于哪些服务(此依赖是”强依赖”,一旦所依赖的服务异常,当前的服务也随之停止)
Wants 指定依赖于哪些服务(此依赖是”弱依赖”,所依赖的服务异常,不影响当前的服务)
Conflicts 定义units间的冲突关系
Wants=:Configures (weak) requirement dependencies on other units.However, if the listed units fail to start or cannot be added to the transaction, this has no impact on the validity of the transaction as a whole, and this unit will still be started.
Requires=:Similar to Wants=, but declares a stronger requirement dependency.If this unit gets activated, the units listed will be activated as well. If one of the other units fails to activate, and an ordering dependency After= on the failing unit is set, this unit will not be started. Besides, with or without specifying After=,this unit will be stopped if one of the other units is explicitly stopped.

2、[Service]
根据 unit 类型使用相对应的设置项目,拿service当范本,主要规范服务启动的脚本、环境配置文件文件名、重新启动的方式等等,systemctl的管理单位是unit,不仅仅管理service也管理其他比如socket、 device、mount、swap、target、path、timer、slice、scope,service只是systemctl的管理单位unit的一种,Service常用选项:
可选项 描述
Type Configures the process start-up type for this service unit.
simple:默认值,这个服务主要由 ExecStart 设置的程序来启动,启动后常驻于内存中。
forking:由 ExecStart 指定的启动的程序通过spawns产生子进程提供服务,然后父进程退出。
Environment 为服务添加环境变量
EnvironmentFile 指定当前服务启动的环境变量配置文件
ExecStart 指定服务启动时执行的命令或脚本,接受 “命令 参数 参数…” 的格式,不能接受 <, >, >>, |, & 等特殊字符,很多的bas语法也不支持。所以,要使用这些特殊的字符时,最好直接写入到脚本里或借助“/bin/bash -c”将一个长字符串当作一条完整的命令来执行比如ExecStart=/bin/bash -c “/usr/bin/influxd &”
ExecStartPre 指定服务启动前执行的命令或脚本
ExecStartPost 指定服务启动后执行的命令或脚本
ExecStop 指明停止服务要运行的命令或脚本
ExecStopPost 指定服务停止后执行的命令或脚本
RestartSec 指定服务在重启时等待的时间,单位为秒
ExecReload 指明重新加载服务要运行的命令或脚本
Restart Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached.配置当服务进程退出、被终止或超时时是否重新启动服务
no:退出后不会重启
on-success:当进程正常退出时(退出码为0) 执行重启
on-failure:当进程不正常退出时(退出码不为0) 执行重启
on-abnormal:当被信号终止和超时执行重启
always:一直重启
on-abort:当收到没有捕捉到的信号终止时执行重启
User 运行服务的用户,会影响服务对本地文件系统的访问权限
Group 运行服务的用户组,会影响服务对本地文件系统的访问权限
TimeoutStartSec 设定该服务允许的最大启动时长,设为0,表示永不超时
Type =:If set to simple (the default if ExecStart= is specified but neither Type= nor BusName= are), the service manager will consider the unit started immediately after the main service process has been forked off. It is expected that the process configured with ExecStart= is the main process of the service. In this mode, if the process offers functionality to other processes on the system, its communication channels should be installed before the service is started up (e.g. sockets set up by systemd, via socket activation), as the service manager will immediately proceed starting follow-up units, right after creating the main service process, and before executing the service’s binary. Note that this means systemctl start command lines for simple services will report success even if the service’s binary cannot be invoked successfully (for example because the selected User= doesn’t exist, or the service binary is missing).
如果设置为 simple(如果指定了 ExecStart= 但 Type= 和 BusName= 均未指定,则为默认值),服务管理器将认为该单元在主服务进程派生后立即启动。预计ExecStart=配置的进程是服务的主进程。在此模式下,如果进程向系统上的其他进程提供功能,则应在服务启动之前安装其通信通道(例如,由 systemd 通过套接字激活设置套接字),因为服务管理器将立即开始执行以下操作-up 单元,在创建主服务进程之后、执行服务的二进制文件之前。请注意,这意味着即使无法成功调用服务的二进制文件(例如,因为所选的 User= 不存在,或者服务二进制文件丢失),简单服务的 systemctl start 命令行也会报告成功。
Type =:If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main service process, and the service manager will consider the unit started when the parent process exits. This is the behavior of traditional UNIX services. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can reliably identify the main process of the service. systemd will proceed with starting follow-up units as soon as the parent process exits.
如果设置为 forking,则预计使用 ExecStart= 配置的进程将调用 fork() 作为其启动的一部分。当启动完成并且所有通信通道都建立后,父进程预计将退出。子进程继续作为主服务进程运行,当父进程退出时,服务管理器会认为该单元已启动。这是传统 UNIX 服务的行为。如果使用此设置,建议同时使用PIDFile=选项,以便systemd能够可靠地识别服务的主进程。一旦父进程退出,systemd 将继续启动后续单元。
Restart:=If set to no (the default), the service will not be restarted. 如果设置为 no(默认值),则不会重新启动该服务。
Restart:=If set to on-success, it will be restarted only when the service process exits cleanly.如果设置为on-success,只有当服务进程干净退出时才会重新启动。
Restart:=If set to on-failure, the service will be restarted when the process exits with a non-zero exit code, is terminated by a signal (including on core dump, but excluding the aforementioned four signals), when an operation (such as service reload) times out, and when the configured watchdog timeout is triggered.如果设置为 on-failure,当进程以非零退出代码退出、被信号终止(包括核心转储,但不包括上述四个信号)、当某个操作(例如服务reload) 超时,以及触发配置的看门狗超时,服务将重新启动。
Restart:=If set to on-abnormal, the service will be restarted when the process is terminated by a signal (including on core dump, excluding the aforementioned four signals), when an operation times out, or when the watchdog timeout is triggered. 如果设置为on-abnormal,则当进程被信号终止时(包括core dump,不包括上述四个信号)、操作超时或触发看门狗超时时,服务将重新启动。
Restart:=If set to on-abort, the service will be restarted only if the service process exits due to an uncaught signal not specified as a clean exit status. 如果设置为 on-abort,则仅当服务进程由于未指定为干净退出状态的未捕获信号而退出时,服务才会重新启动。
Restart:=If set to always, the service will be restarted regardless of whether it exited cleanly or not, got terminated abnormally by a signal, or hit a timeout.如果设置为always,则无论服务是否干净退出、是否因信号异常终止或超时,服务都将重新启动。

3、[Install]
将 unit 安装到那个target里面去的意思,Install常用选项:
Install是服务的安装信息,它不在 systemd 的运行期间使用,只在使用 systemctl enable 和 systemctl disable 命令启用/禁用服务时有用,所有 Unit 文件通用,用来定义如何启动,以及是否开机启动,参数详解如下:
WantedBy:这个unit附挂在哪一个 target unit 下面的,一般来说,大多的服务性质的 unit 都是附挂在 multi-user.target下面。它的值是一个或多个 target,执行enable命令时,符号链接会放入/etc/systemd/system目录下以target 名 + .wants后缀构成的子目录中。“WantedBy=multi-user.target” 表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。当然还需要 systemctl enable 激活这个服务以后自动运行才会生效
RequiredBy:依赖当前服务的模块。它的值是一个或多个 target,执行enable命令时,符号链接会放入/etc/systemd/system目录下以 target 名 + .required后缀构成的子目录中
Also:当前 Unit 被 enable/disable 时,会被同时操作的其他 Unit
Alias:当前 Unit 可用于启动的别名

例子,配置datadomain存储在ubuntu上开机自动挂载

**root@DAILAPGDBUP001:~# cat /etc/issue
Ubuntu 22.04.1 LTS \n \l
root@DAILAPGDBUP001:~# cat /root/mountdatadomaindir.sh
/opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# vim /usr/lib/systemd/system/mountdatadomaindir.service
[Unit]
Description=mountdatadomaindir
After=network.target
[Service]
User=root
Group=root
Type=forking
ExecStart=/bin/bash /root/mountdatadomaindir.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
root@DAILAPGDBUP001:~# systemctl enable mountdatadomaindir
root@DAILAPGDBUP001:~# reboot
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.1G   65G  12% /
tmpfs                              7.8G  1.1M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  249M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  1.2G  149G   1% /PGDATA
boostfs                            377T  287T   90T  77% /mnt/datadomaindir
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
root@DAILAPGDBUP001:~# systemctl status mountdatadomaindir
● mountdatadomaindir.service - mountdatadomaindir
     Loaded: loaded (/lib/systemd/system/mountdatadomaindir.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-09-15 09:03:32 UTC; 2min 41s ago
    Process: 943 ExecStart=/bin/bash /root/mountdatadomaindir.sh (code=exited, status=0/SUCCESS)
   Main PID: 1091 (boostfs)
      Tasks: 4 (limit: 19009)
     Memory: 13.6M
        CPU: 80ms
     CGroup: /system.slice/mountdatadomaindir.service
             └─1091 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomain_archivelog -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# systemctl stop mountdatadomaindir
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.1G   65G  12% /
tmpfs                              7.8G     0  7.8G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  249M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  1.2G  149G   1% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
root@DAILAPGDBUP001:~# systemctl start mountdatadomaindir
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.1G   65G  12% /
tmpfs                              7.8G  1.1M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  249M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  1.2G  149G   1% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
boostfs                            377T  287T   90T  77% /mnt/datadomaindir**

Restart=on-failure时,systemctl stop mountdatadomaindir后不会自动起来,kill也不会起来(/mnt/datadomaindir目录不见了)

root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1806708       1  0 11:28 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# kill 1806708
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.2G   65G  12% /
tmpfs                              7.8G  5.5M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  251M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  6.4G  144G   5% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0

Restart=on-success时,systemctl stop mountdatadomaindir后不会自动起来,kill会自动起来(/mnt/datadomaindir目录看见了)

root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1807834       1  0 11:34 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# kill 1807834
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.2G   65G  12% /
tmpfs                              7.8G  4.3M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  251M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  6.5G  144G   5% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
boostfs                            377T  302T   76T  81% /mnt/datadomaindir
root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1807932       1 10 11:35 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true

Restart=always时,systemctl stop mountdatadomaindir后不会自动起来,kill会自动起来(/mnt/datadomaindir目录看见了)

root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1809107       1  3 11:41 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# kill 1809107
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.2G   65G  12% /
tmpfs                              7.8G  4.3M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  251M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  6.4G  144G   5% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
boostfs                            377T  302T   76T  81% /mnt/datadomaindir
root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1809197       1  0 11:42 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值