pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

Ubuntu Startup Script - Not Working

Open ishanrakitha opened this issue 12 years ago • 26 comments

I'm using ubuntu server 12.10. i have genarated the start up script and executed the given command, but i have to manually execute "pm2 restart all" to get my process online. I can see my process in pm2 after system start up. it show as online. but it's not.

ishanrakitha avatar Feb 04 '14 13:02 ishanrakitha

Which nodejs version do you use ? pm2 version ? Have you already generated an init script with older pm2 version ?

Unitech avatar Feb 04 '14 14:02 Unitech

My node version is - v0.10.24

just now i removed my startup script using "sudo update-rc.d -f pm2-init.sh remove" and reinstalled pm2 latest version. still the same.

ishanrakitha avatar Feb 04 '14 14:02 ishanrakitha

Can you do something like pm2 dump; pm2 kill; pm2 resurrect and tell me if processes are up ?

Unitech avatar Feb 05 '14 10:02 Unitech

yes all the processes are up.

ishanrakitha avatar Feb 05 '14 16:02 ishanrakitha

Have you managed to fix this issue ?

Unitech avatar Feb 12 '14 13:02 Unitech

no, still I'm manually restarting pm2 after a system reboot. I think the issue is in my ubuntu installation. To be sure I ll test pm2 and my services in a fresh ubuntu server and see.

ishanrakitha avatar Feb 12 '14 13:02 ishanrakitha

I have the exact same problem on Ubuntu 12.04 with Node v0.10.25. It seems that the processes are not getting dumped, so there's nothing to resurrect.

pstadler avatar Feb 17 '14 09:02 pstadler

Had this issue too (debian 7),

Workaround : I've edited the /etc/init.d/pm2.sh and edited to pm2 start path/to/script

It seems that the dumped result is lost after rebooting. Dump/kill/resurrect are working fine when executed.

soyuka avatar Feb 17 '14 09:02 soyuka

That's the obvious solution but this fancy dump / resurrect method should still be fixed.

pstadler avatar Feb 17 '14 10:02 pstadler

+1 Just tried again with dump/resurect in the init.d/pm2-init.sh and it's working (Debian 7).

The pm2 script has to be running when the shutdown takes place (or it won't be dumped). Only one restart script for pm2 should be provided.

soyuka avatar Feb 17 '14 10:02 soyuka

if i have multiple processes i have set my path to "processes.json" right ? or is it possible to add mutiple services to "pm2-init.sh" ? as you mentioned earlier.

ishanrakitha avatar Feb 18 '14 18:02 ishanrakitha

It looked like this : https://gist.github.com/soyuka/9080276

And take a look at the update-rc.d command if you're on ubutun/debian to make your own.

But the dump/resurrect method is working.

soyuka avatar Feb 18 '14 21:02 soyuka

I was able to fix the problem. It seems that I was in the wrong directory while running the command to setup the init script. Everything works fine after setting the correct path to the user's home directory in /etc/init.d/pm2-init.sh:

export HOME="/home/www"

I hope this helps.

pstadler avatar Feb 23 '14 15:02 pstadler

In my server "export HOME="/root" was already set. temporarily I'm using "su - root -c "pm2 resurrect" in my "/etc/rc.local".

ishanrakitha avatar Feb 23 '14 17:02 ishanrakitha

So, did you figure a way to make it work?

soyuka avatar Feb 24 '14 08:02 soyuka

No I'm not. Using rc.local to resurrect my process on startup.

ishanrakitha avatar Feb 24 '14 09:02 ishanrakitha

If it helps, I think I have a repro of this issue that can be created easily in vagrant: https://github.com/fschwiet/helloVagrant/tree/pm2-issue42900019

  1. install vagrant and virtualbox
  2. clone the repository
  3. run "vagrant up nodejs"
  4. Observe that http://192.168.33.11:8080/ returns a hello world message
  5. run "vagrant reload nodejs"
  6. Observe that http://192.168.33.11:8080/ fails (expected result: a hello world message)

Here is a snippet from the Vagrantfile:

config.vm.define "nodejs" do |nodejs|

    nodejs.vm.network "private_network", ip: "192.168.33.11"

    nodejs.vm.provision :chef_solo do |chef|
        chef.cookbooks_path = "cookbooks"
        chef.add_recipe "nodejs::install_from_binary"
        chef.add_recipe "nodejs::npm"

        chef.json = {
            :nodejs => {
                version: "0.10.6",
                checksum_linux_x64: "cc7ccfce24ae0ebb0c50661ef8d98b5db07fc1cd4a222c5d1ae232260d5834ca"
            }   
        }
    end

    nodejs.vm.provision "shell", inline: "sudo npm install pm2 -g"
    nodejs.vm.provision "shell", inline: "cd /vagrant/src/server; pm2 start hello-server.js"
    nodejs.vm.provision "shell", inline: "cd ~;  pm2 startup ubuntu;"
  end

fschwiet avatar May 12 '14 23:05 fschwiet

Thank you :)

ishanrakitha avatar May 25 '14 17:05 ishanrakitha

For reference to those running into this kind of issue, to keep pm2 running after reboot, I am using crontab:

(crontab -l ; echo '@reboot /sites/provision.sites.sh') | crontab

Where provision.sites.sh is something like:

if [ $(ps -e -o cmd | grep pm2 | grep hello-server | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
then
    cd /sites/www; 
    /usr/local/bin/pm2 start hello-server.js --user vagrant
fi

fschwiet avatar May 25 '14 18:05 fschwiet

noted and thanks :)

ishanrakitha avatar May 25 '14 18:05 ishanrakitha