Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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