1、service配置文件
配置文件路径
/etc/systemd/system
[Unit]
Description=API Service
After=network.target
[Service]
Type=simple
User=root
ExecStart=your python path your script path
WorkingDirectory=工作路径
Restart=always # 任何情况都重启
RestartSec=10s # 重启时间间隔
StandardOutput=append:日志输出文件
StandardError=inherit
[Install]
WantedBy=multi-user.target
2、常用命令
systemctl daemon-reload # 重新加载配置参数
systemctl --help
journalctl -xu xxxx -fn 200 # 查看xxxx.service的日志
3、真实配置案例
[Unit]
Description=API Service
After=network.target
[Service]
Type=simple
User=root
ExecStart=/root/venv/bin/python /root/consumer.py
WorkingDirectory=/root/spider/
Restart=always
RestartSec=10s
StandardOutput=append:/data/consumer.log
StandardError=inherit
[Install]
WantedBy=multi-user.target