linux debain systemd 開機啓動 nodejs 兼容原initd啓動 forever 開機自啓

佈署環境爲debian 7.5node

佈署到一臺新機器,系統版本爲debian 8.0linux

原啓動項web

/etc/init.d/mongo_servicedocker

開機竟然沒法自起shell

 

開機自啓動不能用了,看頁面輸出就發覺不對勁,肯定是systemd,以前搭建部署coreos(也是用的systemd) docker kubernetes接觸過,很親切,只是遷移要費點時間。api

 

原來debian升級8.0後,改成用systemd管理啓動項。spa

 

查資料知,兼容原啓動管理,systemd 會自動生成 unit。日誌

輸入code

systemctl enable mongo_serviceblog

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
相關文章
相關標籤/搜索