Skip to content

Instantly share code, notes, and snippets.

@ray-moncada
Forked from gmcclins/vagrant-hostname-down.sh
Created December 24, 2021 14:30
Show Gist options
  • Save ray-moncada/f3fa95906bc5d5b483d41ae7132f0906 to your computer and use it in GitHub Desktop.
Save ray-moncada/f3fa95906bc5d5b483d41ae7132f0906 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment