Peťan Peťan - 10 months ago 74
Bash Question

Start Vagrant VM and start Node.js script by shell script

I installed StackEdit on Vagrant. I would like to start Vagrant and StackEdit by one click.
I created bash script:


vagrant up

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

start "C:\Program Files\Mozilla Firefox\firefox.exe"

in VM

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

exit 0

If I run
via ssh manualy everything works, but when I try it with ssh in start script - now commented line - server doesn't run.

I tried copy this script to
, but the result is same.
I tried add
@reboot /home/vagrant/Code/
crontab -e
too, but without success.

Can anyone help me?

My system is Windows 10. I use Git Bash.

Answer Source

you should put everything in your Vagrantfile

Run provisioning

You can run your script from Vagrantfile using a shell provisioner

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

check, you have some options by default it will run as root so you can change if you want to run your script as vagrant user

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

and also you can make sure you run your script everytime you boot the VM (by default it runs only once or when calling specifically the provision parameter)

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

opening the website after the system is running

Vagrantfile is a ruby script so you can call any command from the file, but it will run the command immediately and in any occasion.

Then, if you want to run after the box is started, you can use the vagrant trigger plugin and do something like

Vagrant.configure(2) do |config|
  config.trigger.after :up do
    system("open", "")