Skip to content

Instantly share code, notes, and snippets.

@gmcclins
Created May 26, 2015 15:57
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gmcclins/171ba15a2c22b53c3f21 to your computer and use it in GitHub Desktop.
manage a vagrant host with systemd
#!/usr/bin/env bash
#
# script to stop vagrant box for use with systemd
# place scripts into /usr/lib/systemd/scripts/
# be sure to put the .service file in /lib/systemd/system/
#
# -- Geoffrey McClinsey
VAGRANTUSR=vagrant # user you want vagrant box to run as
VAGRANTBOXPATH= # add path to Vagrantfile
cd ${VAGRANTBOXPATH}
su -c "/usr/bin/vagrant halt" ${VAGRANTUSR} > /dev/null 2>&1
exit 0
#!/usr/bin/env bash
#
# script to start vagrant box for use with systemd
# place scripts into /usr/lib/systemd/scripts
#
# -- Geoffrey McClinsey
VAGRANTUSR=vagrant # user you want vagrant box to run as
VAGRANTBOXPATH= # add path to Vagrantfile
cd ${VAGRANTBOXPATH}
su -c "/usr/bin/vagrant up" ${VAGRANTUSR} > /dev/null 2>&1
exit 0
# vagrant service file to start/stop a specific box
# be sure to adjust ExecStart and ExecStop to match the name of scripts
# located in /usr/lib/systemd/scripts/
#
# Once all files are in place use systemctl enable vagrant-hostname.service
# and then you can sudo service start/stop
#
# -- Geoffrey McClinsey
[Unit]
Description=power-on/off vagrant box
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/vagrant-hostname-up.sh
ExecStop=/usr/lib/systemd/scripts/vagrant-hostname-down.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
@TheMightyJonez
Copy link

Worked perfectly for what I was looking for! Tried just executing the up/halt command within the service file and it does not work. This is the best solution, thank you Geoffrey!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment