Skip to content

Automatically start at boot PM2 & Systemd

tagyoureit edited this page May 7, 2021 · 9 revisions

PM2, Systemd, init.d

PM2

Thanks to @rerouted How to get PM2 installed and running poolController on RPi or Linux

Environment File method

  1. Install PM2 with sudo npm install -g pm2
  2. Quit nodejs-poolController if its running.
  3. Put the script below in your home directory and call in ecosystem.config.js. If you do not need all three apps (REM, dashPanel, nodejs-poolController), simply delete the sections between/including the {} for the app that you do not want to start.
  4. pm2 start ecosystem.config.js
  5. pm2 save
  6. Run pm2 ls to see your app(s) running with pm2. It will show uptime of the app and reset counter. pm2 monit will bring up a UI for monitoring the same.
  7. Type pm2 startup If you want pm2 to auto-start at boot (recommended). This command will give you a command based on your platform to run so pm2 starts on its own. Make sure you follow the directions and paste the given command back into your command line. pm2 unstartup will reverse this behavior.
    • Optional, but highly recommended. Install logrotate to make sure that your logs don't consume all of your disk space.

The below scripts have been updated 5/2021 for some newer pm2 features and better startup/restart strategies.

  • pm2 will only watch directories with source files (whitelist) instead of excluding directories with files that change
  • restart delay is set at 10s
  • watch_delay will restart the app within 5s of seeing source files changed
  • the scripts will recompile the app each and every time it starts
module.exports = {
  apps : [
    {
      "name": "REM",
      "script": "npm",
      "args": [
        "start"
      ],
      "cwd": "/home/pi/relayEquipmentManager",
      "restart_delay": 10000,
      "watch": [
        "boards",
        "config",
        "connections",
        "devices",
        "gpio",
        "i2c-bus",
        "logger",
        "pages",
        "pinouts",
        "spi-adc",
        "web",
        "package.json"
      ],
      "watch_delay": 5000
    },
    {
      "name": "dashPanel",
      "script": "npm",
      "args": [
        "start"
      ],
      "cwd": "/home/pi/nodejs-poolController-dashPanel",
      "restart_delay": 10000,
      "watch": [
        "pages",
        "scripts",
        "server",
        "themes",
        "packge.json"
      ],
      "watch_delay": 5000
    },
    {
      "name": "njsPC",
      "script": "npm",
      "args": [
        "start"
      ],
      "cwd": "/home/pi/nodejs-poolController",
      "restart_delay": 10000,
      "watch": [
        "config",
        "controller",
        "logger",
        "web",
        "package.json"
      ],
      "watch_delay": 5000
    }
  ]
};

pm2 logs is useful for looking at the console logs of your app(s) for troubleshooting.

Systemd

Submitted by @dkossman This systemd setup worked for me on Raspbian - can you add to the README? Seems like it might be helpful for others. This is based on a gitter post by @guru-florida, slightly adjusted for my configuration.

pi> sudu vi /etc/systemd/system/poolController.service # or your favorite editor

this file should contain the following. You may need to edit to adjust the user, working directory, or start command for your installation and OS:

[Unit]
Description=NodeJS Pool Controller
Documentation=https://github.com/tagyoureit/nodejs-poolController/
After=network.target

[Service]
Environment=NODE_ENV=production
Type=simple
User=pi
WorkingDirectory=/home/pi/nodejs-poolController
ExecStart=/usr/local/bin/node dist/app.js
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

Then run these commands:

pi> sudo systemctl daemon-reload
pi> sudo systemctl enable poolController
pi> sudo systemctl start poolController

To check the status of the service: pi> systemctl status poolController

To tail the log: pi> sudo -n journalctl -o cat -n 2500 -f -u poolController # tail the poolController log, -u option is based on service name

To stop, restart or disable the service, use the appropriate systemctl command:

pi> sudo systemctl stop poolController
pi> sudo systemctl restart poolController
pi> sudo systemctl disable poolController

Another alternative method

Props to @antamy. Another approach to an etc/init.d script. The script is runAtBoot.sh. See https://github.com/chovy/node-startup for instructions to use this script.

Clone this wiki locally