Automatically start at boot PM2 & Systemd - tagyoureit/nodejs-poolController GitHub Wiki

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 npm install -g pm2
  2. Quit nodejs-poolController if its running.
  3. Put the script below in your home directory and call it 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,
      "kill_timeout": 15000
    },
    {
      "name": "dashPanel",
      "script": "npm",
      "args": [
        "start"
      ],
      "cwd": "/home/pi/nodejs-poolController-dashPanel",
      "restart_delay": 10000,
      "watch": [
        "pages",
        "scripts",
        "server",
        "package.json"
      ],
      "watch_delay": 5000,
      "kill_timeout": 15000
    },
    {
      "name": "njsPC",
      "script": "npm",
      "args": [
        "start"
      ],
      "cwd": "/home/pi/nodejs-poolController",
      "restart_delay": 10000,
      "watch": [
        "config",
        "controller",
        "logger",
        "web",
        "package.json"
      ],
      "watch_delay": 5000,
      "kill_timeout": 15000
    }
  ]
};

If Pi is not your username

From @FlaMike - https://github.com/tagyoureit/nodejs-poolController/discussions/801#discussioncomment-7809175

Success at long last with pm2!!! For the benefit of others, this is what I had to do.

First, the path names in the ecosystem.config.js must match your home directory. If you've changed the name of your pi, you must update the ecosystem.config.js to match your home directory in all of the "cwd" lines. In my case, the home directory was "michael" for my login name. So in each of the three "cwd" lines, make sure your absolute directory names match your home directory. In my case, I replaced 'pi' with 'michael'. You must do this before starting pm2, lest you wind up with the mess I had. e.g., "cwd": "/home/michael/relayEquipmentManager",

I had not done the above--I used the mode file verbatim. Hence I had the pid errors noted above.

If you mess up like I did, do the following in each of the 3 program directories (nodejs-poolController, nodejs-poolController-dashPanel, and relayEquipmentManager):

remove the node-modules directory remove the package-lock.json file run npm install From your home directory: uninstall pm2 by doing the following: pm2 unstartup pm2 kill npm remove pm2 -g rm -rf ~/.pm2

You can then reinstall pm2, but make sure the ecosystem.config.js is correctly pointing to your home directory first!!!! And now you should be good to go forward.

Logs

pm2 logs or pm2 monit 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/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.