vagrant 上的 PM2 - 在安装共享文件夹后启动应用程序

PM2 on vagrant - starting app AFTER shared folder is mounted

如何设置PM2在共享目录挂载后启动应用程序?默认情况下 pm2 startup 添加脚本,它会在 OS 启动后立即尝试 运行 脚本,这会导致程序错误(因为那时文件夹尚未安装)。

您可以在 Vagrantfile 中添加以下行

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

了解 shell provisioning :

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

这是您输入命令行的地方,就像您通过 ssh 进入框时输入命令行一样

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

在你的情况下,设置为 false 这样 vagrant 用户将 运行 这个命令

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"

设置为 always 这样 pm2 将在您启动 VM 的任何时候启动

如果你想运行多个命令你也可以这样写

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