Systemd服务管理 - Linux服务配置详解

Systemd是现代Linux系统的主流服务管理工具,取代了传统的SysV init系统。

一、服务管理命令

systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl enable nginx
systemctl disable nginx
systemctl enable --now nginx

二、Service Unit配置

[Unit]
Description=My Application Service
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/main.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

三、Timer定时任务

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target

四、查看日志

journalctl -u nginx
journalctl -u nginx -f
journalctl --since "1 hour ago"
journalctl -k

掌握Systemd是Linux运维的必备技能!

发表回复

后才能评论