Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PM2 on vagrant - starting app AFTER shared folder is mounted

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).

like image 606
Luke Avatar asked Mar 12 '23 19:03

Luke


1 Answers

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
like image 75
Frederic Henri Avatar answered Apr 07 '23 02:04

Frederic Henri