23

I am new to vagrant and have set up a couple of vagrant vm's which I use for development and testing purposes. My issue is that I cant get these boxes to start up automatically on my machine (Ubuntu). It is really annoying to go to the folder and vagrant up each machine every time my host machine starts up.

I have tried adding a cronjob that looks like cd path/to/vm/folder && vagrant up but this does not seem to be working.

I also tried a cronjob for VBoxManage but vagrant changes the name of the VM (rather the number/version 'vmname_version') everytime the VM boots up.

4 Answers 4

18

Cron job doesn't fit into this use case, it is for scheduled jobs.

As you are running Ubuntu as host, I'd recommend using /etc/rc.local, place the commands in the rc.local script which is executed at the end of the init process.

It'll look like this

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.    
cd /path/to/vagrant_vm1 && vagrant up
cd /path/to/vagrant_vm2 && vagrant up
exit 0

NOTE: There will be port conflicts on the host if you start more than 1 Vagrant boxes with the same networking mode - NAT (default), which by default use the same port forwarding rule => guest 22 to host 2222.

If you need to start more than 1 boxes (NAT), consider using public network (bridged) or use VBoxManage controlvm to start the VMs, refer to the answer in Two separate Vagrant machines, windows host, PuTTY - how?

6
  • In theory that is what I wanted however Vagrant seems to be having some issues with this, It reaches "Waiting for VM to boot" but does not successfully boot it, it ends up with an error saying "Failed to connect to VM via SSH. Please verify the VM successfully booted by looking at the VirtualBox GUI.". Thanks Anyway! Aug 8, 2013 at 14:48
  • @ShrikantPatnaik See updated answer. I think you are hitting the port conflict issue by starting multiple vagrant boxes.
    – Terry Wang
    Aug 9, 2013 at 1:43
  • Naa, Ports and everything are configured right because I have all of them running simultaneously with no issues, the controlvm worked, but I had to clone them as I replied in my own answer to the question. Aug 12, 2013 at 6:07
  • Well, if you use default NAT mode and start using vagrant up it'll try to do the port forwarding anyway (guest 22 => host 2222) for all VMs, there will be conflicts but I realized it may NOT be bad enough to prevent VMs from being started.
    – Terry Wang
    Aug 14, 2013 at 2:35
  • 1
    This worked for me in ubuntu 16.04 after specifying full path to vagrant command and running vagrant from the user, that vagrant was initially used with. The command looks like this: cd /path/to/vagrant_vm1 && su -c "/usr/bin/vagrant up" myuser Feb 8, 2017 at 10:52
7

Here is a startup script found at http://uhowto.urbylog.info/post/54097294891/an-upstart-script-to-launch-vagrant-box-automatically

An upstart script to launch vagrant box automatically Sometimes it could be necessary to start vagrant virtual machines authomatically on host boot. Similarly, providing a graceful shutdown for a guest when the host is being turned off or restarted. Below is an example of an upstart script for launching/halting a vagrant box on behalf of randomjoe user. Change user name and Vagrantfile dir in the script below. Name your upstart configuration file descriptively. Create one /etc/init/*.conf file for each Vagrantfile you need up and running at system boot. /etc/init/vagrant-precise32.conf:

description "Start vagrant precise32 box on system startup"
author "uHOWTO"
env VAGRANTUSR=randomjoe
env VAGRANTBOXPATH=/home/randomjoe/vagrantboxes/precise32
start on stopped rc
stop on runlevel [016]

pre-start script
    cd ${VAGRANTBOXPATH}
    su -c "/usr/bin/vagrant up" ${VAGRANTUSR}
end script

post-stop script
    cd ${VAGRANTBOXPATH}
    su -c "/usr/bin/vagrant halt" ${VAGRANTUSR}
end script
1

I'v ended up with a simple cron job. I had to put in a sleep, otherwise Virtualbox complained that it was not correctly initialized.

@reboot cd /home/myuser/VMs/VagrantBox && sleep 20; /bin/su -c "/usr/bin/vagrant up" myuser
0

I ended up cloning the VM's with VboxManage and just using VBoxManage startvm $vm --type headless in a init.d script

2
  • Now that you are happy with VBoxManage or VBoxHeadless, vagrant is out of the picture anyway.
    – Terry Wang
    Aug 14, 2013 at 2:37
  • Pretty much, but i do think it is slightly hacky. Aug 20, 2013 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.