带有依赖关系的服务开机自启配置
比如:leanote笔记需要同时启用mongodb以及leanote服务, 但是二者有先后顺序,mongodb在前,leanote在后。
主要步骤如下:
cd /etc/systemd/system
vi mongodb.service
[Unit]
Description=MongoDB Service
After=network.target
[Service]
Type=simple
ExecStart=/opt/install/leanote/mongodb-linux-x86_64-amazon-3.6.3/bin/mongod --dbpath=/opt/install/leanote/data
ExecStop=/bin/kill -9 $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
vi leanote.service
最关键步骤:这里Environment配置与/etc/profile中环境变量保持一致
[Unit]
Description=Leanote Service
After=mongodb.service
Requires=mongodb.service
[Service]
Type=simple
Environment="GOPATH=/opt/install/leanote/gopackage/leanote-all-master"
Environment="GOROOT=/opt/install/leanote/go"
Environment="PATH=$PATH:/opt/install/leanote/go/bin:/opt/install/leanote/gopackage/leanote-all-master/bin"
ExecStart=/opt/install/leanote/gopackage/leanote-all-master/bin/revel run github.com/leanote/leanote
ExecStop=/bin/kill -9 $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
然后执行一下步骤:
systemctl daemon-reload
systemctl enable mongodb
systemctl enable leanote
# 验证是否开机启动
systemctl is-enabled mongodb
systemctl is-enabled leanote
# 启动服务
systemctl start mongodb
systemctl start leanote