繁体   English   中英

启动 Vagrant VM 并通过 shell 脚本启动 Node.js 脚本

[英]Start Vagrant VM and start Node.js script by shell script

我在 Vagrant 上安装了 StackEdit。 我想一键启动 Vagrant 和 StackEdit。 我创建了 bash 脚本:

#!/bin/bash

vagrant up

#ssh -p 2222 -i /d/stackedit/.vagrant/machines/default/virtualbox/private_key vagrant@127.0.0.1 -t '/home/vagrant/Code/start_server.sh'

start "C:\Program Files\Mozilla Firefox\firefox.exe" http://stackedit.app:5000

和虚拟机中的start_server.sh

if [ $(ps -e|grep node|wc -l) = "0" ] ; then
    (export PORT=5000 && node Code/Project/public/stackedit/server.js) &
fi
sleep 5

exit 0

如果我通过 ssh 手动运行start_server.sh一切正常,但是当我在启动脚本中使用 ssh 尝试它时 - 现在注释行 - 服务器不运行。

我尝试将此脚本复制到/ect/rc.local ,但结果相同。 我也尝试将@reboot /home/vagrant/Code/start_server.sh添加到crontab -e ,但没有成功。

任何人都可以帮助我吗?

我的系统是 Windows 10。我使用 Git Bash。

你应该把所有东西都放在你的Vagrantfile

#运行配置

您可以使用shell 配置器从 Vagrantfile 运行脚本

Vagrant.configure("2") do |config|
  config.vm.provision "shell", path: "Code/start_server.sh"
end

检查,默认情况下,您有一些选项,它会以 root 身份运行,因此您可以更改是否要以 vagrant 用户身份运行脚本

Vagrant.configure("2") do |config|
  config.vm.provision "shell", path: "Code/start_server.sh", privileged: false
end

并且您还可以确保每次启动 VM 时都运行脚本(默认情况下它只运行一次或在专门调用provision参数时运行)

Vagrant.configure("2") do |config|
  config.vm.provision "shell", path: "Code/start_server.sh", run: "always"
end

#系统运行后打开网站

Vagrantfile 是一个 ruby​​ 脚本,因此您可以从文件中调用任何命令,但它会在任何情况下立即运行该命令。

然后,如果你想在盒子启动后运行,你可以使用vagrant 触发器并执行类似的操作

Vagrant.configure(2) do |config|
  .....
  config.trigger.after :up do |trigger|
    trigger.run = {inline: 'system("open", "http://stackedit.app:5000"')
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM