node.jspm2

Start Node.js with PM2 on Boot


The whole thing should only take 2 minutes, but of course it doesn't work for me. I tried several versions of PM2 and ran it as root. There were only success messages, but the corresponding service remained on failed and did not start with the system.

This is the message after pm2 startup

[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=send
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/root/.nvm/versions/node/v16.20.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/l                                                        ocal/games:/usr/games:/snap/bin:/usr/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/u                                                        sr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/root/.nvm/versions/node/v16.20.2/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/root/.nvm/versions/node/v16.20.2/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/root/.nvm/versions/node/v16.20.2/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-send.service
Command list
[ 'systemctl enable pm2-send' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-send.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-send...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-send.service → /etc/system                                                        d/system/pm2-send.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

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

This is the status of the service

× pm2-send.service - PM2 process manager
     Loaded: loaded (/etc/systemd/system/pm2-send.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sun 2024-06-09 11:32:40 CEST; 13s ago
       Docs: https://pm2.keymetrics.io/
    Process: 1448 ExecStart=/root/.nvm/versions/node/v16.20.2/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=203/EXEC)
        CPU: 1ms

Jun 09 11:32:40 send systemd[1]: pm2-send.service: Scheduled restart job, restart counter is at 5.
Jun 09 11:32:40 send systemd[1]: Stopped pm2-send.service - PM2 process manager.
Jun 09 11:32:40 send systemd[1]: pm2-send.service: Start request repeated too quickly.
Jun 09 11:32:40 send systemd[1]: pm2-send.service: Failed with result 'exit-code'.
Jun 09 11:32:40 send systemd[1]: Failed to start pm2-send.service - PM2 process manager.

I followed these instructions https://pm2.keymetrics.io/docs/usage/quick-start/

my system is debian and the node.js version 16.x

I tried using my own init, systemd and crontab to alternatively start PM2 when booting the system. None of this worked either

This is my own systemd Code: Failed with error 203

[Unit]
Description=PM2 Send Service
After=network.target
 
[Service]
ExecStart=pm2 start /etc/send/server/bin/prod.js
ExecReload=pm2 reload all
Type=exec
Restart=always
 
 
[Install]
WantedBy=default.target

I tried on the file:

chmod 755
chmod +x

the absolute path is /root/.nvm/versions/node/v16.20.2/lib/node_modules/pm2 but this gives the error code 127, I can't call it via the shell either “directory not found”. via ~/.nvm/versions/node/v16.20.2/lib/node_modules/pm2 it works via the shell but the service gives “Neither a valid executable name nor an absolute path"

systemctl status start-pm2-send.service on my own service gives me:

× start-pm2-send.service - PM2 Send Service
     Loaded: loaded (/etc/systemd/system/start-pm2-send.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sun 2024-06-02 16:39:53 CEST; 12s ago
    Process: 1682 ExecStart=pm2 start /etc/send/server/bin/prod.js (code=exited, status=203/EXEC)
   Main PID: 1682 (code=exited, status=203/EXEC)
        CPU: 1ms
 
Jun 02 16:39:53 send systemd[1]: start-pm2-send.service: Scheduled restart job, restart counter is at 5.
Jun 02 16:39:53 send systemd[1]: Stopped start-pm2-send.service - PM2 Send Service.
Jun 02 16:39:53 send systemd[1]: start-pm2-send.service: Start request repeated too quickly.
Jun 02 16:39:53 send systemd[1]: start-pm2-send.service: Failed with result 'exit-code'.
Jun 02 16:39:53 send systemd[1]: Failed to start start-pm2-send.service - PM2 Send Service.

i don't know which logs you need or information about the service, please just write to me about it


Solution

  • with the help of a friend we were able to write our own systemd service that works

    [Unit]
    Description=Furrysend
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    ExecStart=/root/.nvm/versions/node/v16.20.2/bin/node /etc/send/server/bin/prod.js
    WorkingDirectory=/etc/send/server/bin
    Restart=on-failure
    RestartSec=10
    Type=simple
    
    [Install]
    WantedBy=multi-user.target