systemd 简介
systemd 是 Linux 系统的初始化系统和服务管理器,几乎所有主流发行版都已采用。
核心命令
# 启动服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 查看服务状态
systemctl status nginx
# 重新加载配置(不重启进程)
sudo systemctl reload nginx编写自定义服务单元
创建 /etc/systemd/system/myapp.service:
[Unit]
Description=My Custom App
After=network.target
[Service]
Type=simple
User=www-data
ExecStart=/usr/bin/python3 /opt/myapp/app.py
Restart=always
[Install]
WantedBy=multi-user.target常用操作
systemctl list-units --type=service:列出所有服务systemctl daemon-reload:重载单元文件journalctl -u nginx -f:实时查看服务日志
掌握 systemd 能让你的服务器管理事半功倍。
