Ubuntu 14.04 - pm2 startup not starting after reboot

前端 未结 5 1933
南旧
南旧 2021-01-02 13:04

I am using pm2 to start a node.js process, and I would like this process to be started automatically when the system (Ubuntu 14.04 on Intel Atom processor) is booted. I have

相关标签:
5条回答
  • 2021-01-02 13:25

    Basically,

    Step 1: Run following command,

    pm2 startup
    

    Here you will get 1 command generated, copy/paste it and done.

    (Sample command you will get from above step: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu) Ref

    0 讨论(0)
  • 2021-01-02 13:28

    I been searching for work around for quit while till i came to this solution

    after executing pm2 save

    1. this file will be created -> /etc/init.d/pm2-init.sh
    2. then on cron tab @reboot root /etc/init.d/pm2-init.sh start this equivalent to executing it on cmd but triggered on reboot

    this is working fine for me now

    0 讨论(0)
  • 2021-01-02 13:34

    For pm2 startup to work, there's a critical command buried in documentation and here.

    You have to 1) start all your processes, 2) create the startup script, and 3) run pm2 save

    Reference to @lazlojuly's answer here.

    0 讨论(0)
  • 2021-01-02 13:43

    Well, I think this worked, anyway. I was originally trying to start as a system user (no login shell) called node-red running the node application of the same name, and when that didn't work I tried starting as ordinary user max. After some fiddling with things it suddenly started working, but on this slow Atom processor, the node application starts only a couple of minutes after the desktop appears.

    To get it running under the system user: first, logged in as max, I deleted all pm2 jobs and ran pm2 save to create a empty dump file. Then I did:

    sudo npm install pm2@latest -g
    pm2 update
    

    To get the latest version, in case that helped. I moved from 0.14.7 to 0.15.7

    Then I ran

    sudo su -c "env PATH=$PATH:/usr/bin pm2 startup ubuntu -u node-red"
    

    and edited /etc/init.d/pm2-init.sh correcting the line that sets the PM2_HOME directory to the home directory of node-red:

    export PM2_HOME="/home/node-red/.pm2"
    

    Then I became a login version of the node-red user, changed to that user's home directory, started my job, and saved the process list:

    sudo -H -u node-red bash -l
    cd
    pm2 start /usr/local/bin/node-red --node-args="--max-old-space-size=128" -- -v -u /home/node-red/.node-red
    pm2 save
    

    Then I exited from node-red's shell, rebooted, and (after a minute or so's delay) there was the application running nicely!

    Note that this was ubuntu 14.04, which uses the init/upstart system. Later versions I think use systemd approach and may need the ubuntu parameter changed to systemd when creating the startup script.

    I'm still not really sure why it didn't work properly the first time, though.

    0 讨论(0)
  • 2021-01-02 13:46

    What steps work for me on Ubuntu 18.04.4

    1. npm i pm2 -g
    2. run this command as root user: pm2 startup
      • creates service pm2-root found in /etc/systemd/system
    3. systemctl enable pm2-root, service pm2-root status
      • Note: after Node upgrade, run these commands as root: pm2 unstartup, pm2 startup; documentation
    4. pm2 start dist\index.js --name YourNodeApp
    5. pm2 save
    6. reboot
    7. pm2 ls should show your YourNodeApp online
    0 讨论(0)
提交回复
热议问题