systemd service 基本使用

注意:文章的背景是CentOS Linux 7

什么是systemd service

servicesystemd下的一种配置单元文件,以.service结尾,是systemd下最常用的,也是最重要的配置单元,可以省略其.service结尾,例如:

lua 复制代码
systemctl status sshd

和下面的语句是同一个意思

lua 复制代码
systemctl status sshd.service

此前写过相关文章,但是很多东西融在一块,很难理清楚,这篇文章,单独来详细介绍一下service

早些版本使用SysV initUpstart进行管理任务脚本,其目录为: /etc/rc.d/init.d,这就包括我们熟知的centos 5/6系统。

但是在centos 7中,这些已经被systemd替代,但是还是兼容SysV init管理风格的,例如,在centos 7下有一个服务是network,其脚本路径就是在/etc/rc.d/init.d/network下,但是也可以被systemd管理,图示如下:

从上面截图中可以看到,loaded的配置脚本是/etc/rc.d/init.d/network,而非是以.service结尾的。

同时,也可以使用service和直接调用命令的方式来查看状态,例如:

shell 复制代码
# service network status
# /etc/rc.d/init.d/network status

执行结果如下:

服务操作命令对比

SysV init管理的服务中,我们熟知的2个命令是,servicechkconfig,前则用于启动、停止、重启服务,而后则可以直接操作服务的状态,例如 设置服务开机自启,禁用服务等等。

关于systemctlservice命令对比如下,可以查看如下表格,指的注意的是,使用systemctl操作unit的时候,若不跟类型,则默认类型为service,如下表,systemctl栏位中的name全名应当为name.servicechkconfigsystemctl对比亦然。

描述 systemctl service
启动服务 systemctl start name service name start
停止服务 systemctl stop name service name stop
重启服务 systemctl restart name service name restart
查看服务状态 systemctl status name service name status
重载配置 systemctl reload name service name reload
仅在运行时重启服务 systemctl try-restart name service name condrestart
列出所有服务 systemctl list-units --type service --all service --status-all

chkconfigsystemctl命令对比如下:

描述 systemctl chkconfig
开启服务开机自启 systemctl enable name chkconfig name on
禁止服务开机自启 systemctl disable name chkconfig name off
检查服务是否开机自启 systemctl is-enable name chkconfig --list name

熟悉chkcoinfig命令的同学应该知道,chkconfig允许在运行级别上开启和停用服务,例如:

arduino 复制代码
# chkconfig --level 35 httpd on

上面的意思是在运行界别3和5上面对httpd设置开机自启。但是在systemd中,本身就是模拟的运行级别,所以没有此类设置。

systemctl 服务状态信息

centos 7中,可以使用systemctl status name或者service name status的方式来获取服务状态,后则这是兼容该方式而已,应当使用前则。

比如,查看nginx的服务状态,可以使用如下命令:

arduino 复制代码
# systemctl status nginx

或者

arduino 复制代码
# service nginx status

运行结果截图如下:

可以看到,当service调用查看其nginx的状态的时候,其实是重定向到命令: /bin/systemctl status nginx.service,本质上还是执行desystemd,这是因为centos 7兼容一部分service的命令,作为过度,应该积极使用最新的命令。

对于运行的结果,其日志如下:

yaml 复制代码
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-11-24 15:28:29 CST; 18s ago
  Process: 19179 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 19176 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 19175 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 19181 (nginx)
   CGroup: /system.slice/nginx.service
           ├─19181 nginx: master process /usr/sbin/nginx
           └─19183 nginx: worker process

Nov 24 15:28:29 localhost systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 24 15:28:29 localhost nginx[19176]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 24 15:28:29 localhost nginx[19176]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 24 15:28:29 localhost systemd[1]: Started The nginx HTTP and reverse proxy server.

首先是Loaded,它有3个值,按照分好;进行分割,其中每项的含义是:

  • /usr/lib/systemd/system/nginx.service: 代表服务所读取的配置文件路径

  • disable: 表示禁止开机自启,若为enable则表示会开机自启动,还有一种状态是static表示不能被单独设置为开机自启,不过允许被别的服务调用,换句话说可以作为别的服务的依赖被启动。

  • vendor preset: disabled: 表示在首次安装时,被禁止开机自启

关于vendor preset仅是一个提醒,由服务的管理者设置,提示服务的默认设置。你依然可以使用systemctl enable name来设置服务的开机自启,

而后是Active,它表示服务的运行状态,有以下类型:

  • active(runing): 表示服务正在运行
  • active(exited): 表示服务已经运行完成,且已经退出,多用于一次性服务
  • active(waiting): 表示服务正在等待前置条件,前置条件成功后才会运行该服务
  • active(start): 表示服务正在启动中,并未启动完成
  • active(stop): 表示服务停止中,并未停止完成
  • inactice(dead): 表示服务未运行

而后的信息这里简单列举一下

Main Pid: 它记录的是服务的PID以及其服务名称

Process: 进程的相关附加附近

CGroup: 有关相关控制组cgroup的附加信息

而后便是系统输出的日志,这里仅列出最近的几行而已。

除了使用systemctl status name来查看服务允许状态外,还有如下2个命令可以协助查看:

查看服务是否已经开机自启: systemctl is-enabled name

查看服务当前是否在运行中:systemctl is-active name

使用systemctl查看nginx是否开机自启 和 目前的运行状态(已经提前停止nginx服务)

systemd 如何兼容SysV init脚本

此前曾列举过,使用service对服务进行启停的时候,当检测到是systemd管控的,会重定向到systemctl执行操作,例如:

nginx调用的每一个执行操作,都转换为了systemctl执行,这无可厚非,但是我们若使用该service命令重启网卡的时候,也会调用systemctl执行吗?来看如下结果:

结果展示,它并没有调用systemctl执行,这是因为network服务是属于SysV init类型的,即配置服务路径是在/etc/rc.d/init.d/network下。

所以,systemd在一定程度是兼容SysV init脚本的,于是乎,我们编写了一个最简单的脚本,如下:

bash 复制代码
# cat /etc/init.d/pgsql
#!/bin/bash

case "$1" in
start)
        echo "start pgsql..."
        ;;
stop)
        echo "stop pgsql..."
        ;;
restart)
        echo "restart pgsql..."
        ;;
status)
        echo "status pgsql..."
esac

exit 0
#

该脚本存储的路径为: /etc/init.d/pgsql ,将会获取脚本的第一个参数,而后根据判断进行相应的输出语句,编写完毕后,我们可以立即使用service进行查看,例如:

而直接使用systemctl查看的时候,会报错,报错如下:

这是因为未加载配置单元,使用systemctl daemon-reload加载配置单元后,即可使用systemctl查看状态,例如:

可以利用systemctl执行startstop来查看该状态,其执行结果如下:

注意,systemd支持SysV init,但是它的初衷是为了从SysV init过度到systemd上,而非在systemd上使用该方式。

总结

本篇文章介绍了什么是systemd service,以及和SysV init相关命令做对比,最后再详细分析了一下使用systemctl查看的服务状态,最后聊了一下systemd如何兼容SysV init服务脚本。

本篇文章已经在上述命令对比已经展示了如何对服务进行启动、停止、重启等,所以没有单独开段落介绍此用法。

好咯,今天就这样吧。

相关推荐
2401_867021902 小时前
文件缓冲区(IO与文件 ·III)(linux/C)
linux·运维·服务器·c语言
刘某的Cloud2 小时前
rabbitmq常用命令
linux·运维·分布式·rabbitmq·系统
悄悄敲敲敲2 小时前
Linux:进程间通信->命名管道
linux·运维·服务器
io无心3 小时前
Docker绑定端口报错
运维·docker·容器
悄悄敲敲敲5 小时前
Linux:进程间通信->共享内存
linux·运维·服务器
绵绵细雨中的乡音5 小时前
Linux进程学习【环境变量】&&进程优先级
linux·运维·学习
天下·第二5 小时前
【Nginx】负载均衡配置详解
运维·nginx·负载均衡
GanGuaGua6 小时前
linux:进程的替换
linux·运维·服务器
梓䈑6 小时前
【Linux系统】详解Linux权限
linux·运维·bash
Mr_sun.7 小时前
Day23-Web开发——Linux
linux·运维·服务器