Rasbian Jessie(Pi3)/ Linux上的可靠启动PM2

时间:2017-06-30 16:40:05

标签: linux raspberry-pi3 pm2

我使用其中一个shell脚本(在引导期间以root身份执行rc.local)启动app.get('/users/musicList', function(req, res){ // check with db. // lets say there is some error --> the API token is not valid so user needs to // login if (req was from web) { res.redirect('/signin'); } else { var result = {action : 'SIGNIN'}; res.status(200).json(result); } }); 时启动pm2

以root用户身份

raspberry pi 3

问题/日志

startPm2Root() {
  #delete to avoid all pm2 issues
  rm -rf "$PM2_HOME"
  pkill -f pm2

  USER="root"
  HOME="/root"
  PM2_HOME="$HOME/.pm2"
  export USER HOME PM2_HOME
  pm2 startup systemd &

  if [ -f /home/pi/app/newVersion.tar.gz ]; then
    bash /home/pi/app/deploy.sh
  else
    pm2 kill && pm2 start /home/pi/app/processes.json &
  fi
  pm2 save
}

作为pi

+ USER=root
+ HOME=/root
+ PM2_HOME=/root/.pm2
+ export USER HOME PM2_HOME
+ [ -f /home/pi/app/newVersion.tar.gz ]
+ pm2 startup systemd
+ pm2 save
+ pm2 kill
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid

ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'chmod +x /etc/systemd/system/pm2-root.service',
  'systemctl enable pm2-root',
  'systemctl start pm2-root',
  'systemctl daemon-reload',
  'systemctl status pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
>>> Executing chmod +x /etc/systemd/system/pm2-root.service
[DONE] 
>>> Executing systemctl enable pm2-root
[DONE] 
>>> Executing systemctl start pm2-root
[PM2] PM2 Successfully daemonized
[PM2] Stopping PM2...
[PM2][WARN] No process found
[PM2] All processes have been stopped and deleted
[PM2] PM2 stopped
+ pm2 start /home/pi/app/processes.json
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[DONE] 
>>> Executing systemctl daemon-reload
[DONE] 
>>> Executing systemctl status pm2-root
● pm2-root.service - PM2 process manager
   Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled)
   Active: active (running) since Fri 2017-06-30 16:36:29 UTC; 199ms ago
     Docs: https://pm2.keymetrics.io/
 Main PID: 1034 (PM2 v2.5.0: God)
   CGroup: /system.slice/pm2-root.service
           └─1034 PM2 v2.5.0: God Daemon (/root/.pm2)         

Jun 30 16:36:27 grassberry pm2[1021]: [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
Jun 30 16:36:29 grassberry pm2[1021]: [PM2] PM2 Successfully daemonized
Jun 30 16:36:29 grassberry pm2[1021]: [PM2] Resurrecting
Jun 30 16:36:29 grassberry pm2[1021]: [PM2] Restoring processes located in /root/.pm2/dump.pm2
Jun 30 16:36:29 grassberry pm2[1021]: [PM2][ERROR] No processes saved; DUMP file doesn't exist
Jun 30 16:36:29 grassberry pm2[1021]: ┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────────┐
Jun 30 16:36:29 grassberry pm2[1021]: │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
Jun 30 16:36:29 grassberry pm2[1021]: └──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────────┘
Jun 30 16:36:29 grassberry pm2[1021]: Use `pm2 show <id|name>` to get more details about an app
Jun 30 16:36:29 grassberry systemd[1]: Started PM2 process manager.
[DONE] 
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd

问题

startPm2Pi() { #delete to avoid all pm2 issues rm -rf "$PM2_HOME" pkill -f pm2 USER="pi" HOME="/home/pi" PM2_HOME="$HOME/.pm2" export USER HOME PM2_HOME pm2 startup systemd -u pi & #also tried it without -u if [ -f /home/pi/app/newVersion.tar.gz ]; then bash /home/pi/app/deploy.sh else pm2 kill && pm2 -u pi start /home/pi/app/processes.json & fi pm2 save }

看起来我运行了多个进程:

Error: connect EACCES /home/pi/.pm2/rpc.sock

1 个答案:

答案 0 :(得分:1)

到目前为止看起来很稳定,我删除了启动脚本,我认为它是一个(启动)或另一个(启动)。

startup移除pi的所有片段 sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 unstartup systemd -u pi --hp /home/pi  和root sudo pm2 unstartup

将代码剥离为:

startPm2Root() {
  #delete to avoid all pm2 issues
  rm -rf "$PM2_HOME"
  pkill -f pm2

  USER="root"
  HOME="/root"
  PM2_HOME="$HOME/.pm2"
  export USER HOME PM2_HOME
  #pm2 startup systemd & <= removed this
  pm2 kill && pm2 start /home/pi/app/processes.json &
  #pm2 save <= removed this, launches a second instance somehow
}