张芷铭的个人博客

systemd 是现代 Linux 系统和服务管理器,作为内核启动后第一个进程(PID=1),负责启动流程、服务管理、挂载、定时任务等。

systemctl 是操作 systemd 的命令工具。

核心功能

  • 管理系统开机启动流程
  • 统一管理后台服务、进程、挂载、定时任务
  • 提供并行、带依赖管理的服务运行环境

常用命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 服务管理
systemctl start nginx      # 启动服务
systemctl stop nginx       # 停止服务
systemctl restart nginx    # 重启服务
systemctl status nginx     # 查看状态

# 开机自启
systemctl enable nginx     # 开机自启
systemctl disable nginx    # 取消自启

# 查看服务
systemctl list-units --type=service  # 列出所有服务
systemctl list-unit-files            # 列出所有单元文件

服务单元文件

位于 /etc/systemd/system//lib/systemd/system/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[Unit]
Description=My Service
After=network.target

[Service]
ExecStart=/usr/bin/my-service
Restart=always

[Install]
WantedBy=multi-user.target

重载配置后生效:

1
2
3
systemctl daemon-reload
systemctl enable my-service
systemctl start my-service

详见 [[systemctl]]。

Comments