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).
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--provisionflag is set, as noted above.Optionally, you can configure provisioners to run on every
uporreload. They will only be not run if the--no-provisionflag is explicitly specified. To do this set therunoption 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With