linux debain systemd 开机启动 nodejs 兼容原initd启动 forever 开机自启

布署环境为debian 7.5

布署到一台新机器,系统版本为debian 8.0

原启动项

/etc/init.d/mongo_service

开机居然无法自起

 

开机自启动不能用了,看页面输出就发觉不对劲,确定是systemd,之前搭建部署coreos(也是用的systemd) docker kubernetes接触过,很亲切,只是迁移要费点时间。

 

原来debian升级8.0后,改为用systemd管理启动项。

 

查资料知,兼容原启动管理,systemd 会自动生成 unit。

输入

systemctl enable mongo_service

systemd 会根据原/etc/init.d/mongo_service的内容自动创建一个新的 service 内容如下

 

root@debian:~# systemctl cat mongo_service.service 
# /run/systemd/generator.late/mongo_service.service
# Automatically generated by systemd-sysv-generator

[Unit]
SourcePath=/etc/init.d/mongo_service
Description=LSB: An object/document-oriented database
Before=runlevel2.target runlevel3.target runlevel4.target runlevel5.target shutdown.target
After=network-online.target local-fs.target remote-fs.target nss-lookup.target
Wants=network-online.target
Conflicts=shutdown.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SysVStartPriority=1
ExecStart=/etc/init.d/mongo_service start
ExecStop=/etc/init.d/mongo_service stop

reboot 启动 OK

 

试了试forever 的开机自起。

这东西曾经比较麻烦,写在rc里,执行不起来。

一般的解决办法是是写个shell执行 foreve 再把这个shell加为service 并设置开机启动。

到了systemd这里,非常简单。

只要设个service就行。

内容如下

# /etc/systemd/system/hellowordweb.service
[Unit]
Description=hello wrod web site
After=mongo_service.service
Wants=network-online.target
Conflicts=shutdown.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
ExecStart=/bin/forever start /root/hellowordweb/bin/cluster.js 
ExecStop=/bin/forever stop /root/hellowordweb/bin/cluster.js

 

以下是启动日志

root@debian:~# systemctl status hellowordweb.service

hellowordweb.service - xiaoyun audit web site
   Loaded: loaded (/etc/systemd/system/hellowordweb.service; enabled)
   Active: active (running) since Mon 2015-06-01 06:22:49 EDT; 1s ago
  Process: 1128 ExecStart=/bin/forever start /root/hellowordweb/bin/cluster.js (code=exited, status=0/SUCCESS)
 Main PID: 1133 (node)
   CGroup: /system.slice/hellowordweb.service
           ├─1133 /root/node-v0.12.4-linux-x64/bin/node /lib/node_modules/forever/bin/monitor /root/hellowordweb/bin/cluster.js
           ├─1134 test-xiaoyun-data-api: master
           ├─1135 test-xiaoyun-data-api: worker 1
           └─1136 test-xiaoyun-data-api: worker 2
posted @ 2015-06-01 18:47  cclient  阅读(2158)  评论(1编辑  收藏  举报