systemd unit 与 nginx.service

所属分类:Linux | 浏览:74 | 发布于 2025-12-23

Systemd 可以管理所有系统资源,不同的资源统称为 Unit(单元)。

在Systemd之前有使用initd和service两种方式来管理系统进程,如/etc/init.d/nginx start 和 service start nginx。

Unit 一共分成12种

  • Service Unit:系统服务
  • Target Unit:多个 Unit 构成的一个组
  • Device Unit:硬件设备
  • Mount Unit:文件系统的挂载点
  • Automount Unit:自动挂载点
  • Path Unit:文件或路径
  • Scope Unit:不是由 Systemd 启动的外部进程
  • Slice Unit:进程组
  • Snapshot Unit:Systemd 快照,可以切回某个快照
  • Socket Unit:进程间通信的 socket
  • Swap Unit:swap 文件
  • Timer Unit:定时器

systemctl list-units 命令可以查看当前系统的所有的 Unit。

# 列出正在运行的 Unit
$ systemctl list-units

# 列出所有Unit,包括没有找到配置文件的或者启动失败的
$ systemctl list-units --all

# 列出所有没有运行的 Unit
$ systemctl list-units --all --state=inactive

# 列出所有加载失败的 Unit
$ systemctl list-units --failed

# 列出所有正在运行的、类型为 service 的 Unit
$ systemctl list-units --type=service

其它更多 systemd 的资料可以查看阮一峰的文章:https://ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

 

1、systemd 脚本文件存储路径

1.1、/etc/systemd/system/ : 一般存放用户自定义的service文件

1.2、/lib/systemd/system/ :存放系统范围内标准的service文件

1.3、/usr/lib/systemd/system/ :通过包管理器安装软件时创建的service文件

2、查看 systemd.service 的帮助文档

[root@localhost ~]#man 5 systemd.service

3、Unit 脚本文件格式

一个标准的unit必须包含三个section,[Unit] 、 [Install] 和 [Service],文档中的示例:

[Unit]
Description=Foo

[Service]
ExecStart=/usr/sbin/foo-daemon

[Install]
WantedBy=multi-user.target

4、nginx 脚本文件 nginx.service

[Unit]
Description=nginx service
After=network.target
   
[Service]
Type=forking
ExecStart=/sangfor/server/nginx/sbin/nginx
ExecReload=/sangfor/server/nginx/sbin/nginx -s reload
ExecStop=/sangfor/server/nginx/sbin/nginx -s quit
PrivateTmp=true
   
[Install]
WantedBy=multi-user.target

 

 

海涛贝塔(https://haitaobeta.com)属于海涛个人博客,欢迎浏览使用

联系方式:qq:52292959 邮箱:52292959@qq.com

备案号:粤ICP备18108585号 友情链接