在 Ubuntu 中通过 systemd 自动运行程序
本文以部署 chanify 为例,在不引入 docker 或 pm2 之类的前提下,通过 Ubuntu 自带的 systemd 将 chanify 变成以服务随系统启动,并当挂掉时自动重启。
首先在 /lib/systemd/system/
目录下创建一个 .service
结尾的文件,内容大概如下:
[Unit]
Description=chanify
After=network.target
[Service]
Type=simple
WorkingDirectory=/root/chanify
ExecStart=/root/chanify/chanify serve --config=config.yml
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
然后就可以通过 systemd 的命令来管理服务了:
systemctl enable chanify.service
使服务生效,执行了会随系统启动systemctl start chanify.service
启动服务systemctl stop chanify.service
停止服务systemctl restart chanify.service
重启服务