Cheatsheet for Systemd

Typical Service File

/etc/systemd/system/orzbot.service
[Unit]
Description=orzBot
After=network.target
 
[Service]
User=orzbot
Group=orzbot
SyslogIdentifier=orzbot.service
ExecStart=/home/orzbot/.nvm/nvm-exec node /home/orzbot/orzbot/app.js
WorkingDirectory=/home/orzbot/orzbot
Environment=ORZBOT_TOKEN=ventuaeiornuvyiopwefuvgyu
Environment=NODE_VERSION=8.1.0
Restart=always
RestartSec=5s
KillSignal=SIGINT
TimeoutStartSec=10
TimeoutStopSec=120
 
[Install]
WantedBy=multi-user.target

Commands

  • systemctl daemon-reload 修改配置文件后重载配置,但不影响任何既有进程
  • systemctl start orzbot
  • systemctl stop orzbot
  • systemctl restart orzbot
  • systemctl status orzbot
  • systemctl enable orzbot 根据配置文件中的 Install 安装,典型的是添加到 multi-user.target 目标,简单的说就是开机自启动
  • systemctl disable orzbot 与上行相反
  • journalctl -xe 查看日志
  • journalctl -xef tail 日志
  • journalctl -xe | grep orzbot
  • journalctl -xef | grep orzbot

(disclaimer for grep: has better way, but hard to remember)

See Alsos