systemd 是现代 Linux 系统和服务管理器,作为内核启动后第一个进程(PID=1),负责启动流程、服务管理、挂载、定时任务等。
systemctl 是操作 systemd 的命令工具。
核心功能
- 管理系统开机启动流程
- 统一管理后台服务、进程、挂载、定时任务
- 提供并行、带依赖管理的服务运行环境
常用命令
# 服务管理
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/
[Unit]
Description=My Service
After=network.target
[Service]
ExecStart=/usr/bin/my-service
Restart=always
[Install]
WantedBy=multi-user.target重载配置后生效:
systemctl daemon-reload
systemctl enable my-service
systemctl start my-service详见 systemctl。