bashshellsshvagrantstackedit

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:

#!/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

and start_server.sh in VM

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

exit 0

If I run start_server.sh 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 /ect/rc.local, but the result is same. I tried add @reboot /home/vagrant/Code/start_server.sh to crontab -e too, but without success.

Can anyone help me?

My system is Windows 10. I use Git Bash.


Solution

  • 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/start_server.sh"
    end
    

    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/start_server.sh", privileged: false
    end
    

    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/start_server.sh", run: "always"
    end
    

    #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 and do something like

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