4

How can I set PM2 to start the app after the shared directories get mounted? By default pm2 startup adds script which try to run script right after OS boot, which causes program error (because the folder is not yet mounted by that time).

10
  • how do you start your app ? by default if you use any provisioning from vagrant, they will run after the shared folder are mounted, you can see from output of running vagrant up, you will see Mounting shared folders... before any provisioning step Jul 5, 2016 at 12:01
  • the app in pm2 logs has following error: index-0 (err): chdir(): No such file or directory which appears on vagrant boot so I'm guessing that it's caused by not mounted folders. After manual pm2 stop and pm2 start it's running flawlessly
    – Luke
    Jul 5, 2016 at 12:08
  • how do you start your app ? do you have init script, through vagrant provision ... Jul 5, 2016 at 12:15
  • No I don't. I'm starting the app manually by using cd /vagrant/project/ && npm start (which starts /vagrant/project/server/index.js). I set up pm2 by command: pm2 start /vagrant/project/server/index.js and used pm2 startup but the result is explained above
    – Luke
    Jul 5, 2016 at 13:15
  • so when you do cd /vagrant you're pointing to a shared folder so it is correctly mounted. The issue is not with shared folder being mounted Jul 5, 2016 at 13:43

1 Answer 1

7

you can add the following line in your Vagrantfile

config.vm.provision :shell, :inline => "pm2 start /vagrant/project/server/index.js && pm2 startup", :run => 'always', privileged: false

read about shell provisioning :

  • inline (string) - Specifies a shell command inline to execute on the remote machine.

This is where you will enter the command lines as you enter them when you ssh into the box

  • privileged (boolean) - Specifies whether to execute the shell script as a privileged user or not (sudo). By default this is "true".

In your case, set to false so vagrant user will run this command

By default, provisioners are only run once, during the first vagrant up since the last vagrant destroy, unless the --provision flag is set, as noted above.

Optionally, you can configure provisioners to run on every up or reload. They will only be not run if the --no-provision flag is explicitly specified. To do this set the run option to "always"

setting as always so it pm2 will kick off anytime your boot your VM

If you want to run multiple commands you can also write it like

config.vm.provision "shell", run: "always", privileged: false, inline: <<-SHELL
    pm2 start /vagrant/project/server/index.js
    pm2 startup
    .... any command that you want to execute ....
  SHELL
1
  • 1
    Since it's a problem of compatibility pm2 with vagrant it's more like workaround rather than the solution of problem, but still it fixes the issue so I mark this answer as accepted. Thank you sir
    – Luke
    Jul 6, 2016 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.