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).
1 Answer
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
orreload
. They will only be not run if the--no-provision
flag is explicitly specified. To do this set therun
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
-
1Since 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– LukeJul 6, 2016 at 6:28
vagrant up
, you will see Mounting shared folders... before any provisioning stepindex-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 manualpm2 stop
andpm2 start
it's running flawlesslypm2 start /vagrant/project/server/index.js
and usedpm2 startup
but the result is explained abovecd /vagrant
you're pointing to a shared folder so it is correctly mounted. The issue is not with shared folder being mounted