Skip to content

Instantly share code, notes, and snippets.

@tienthanh2509
Last active April 9, 2017 09:57
Show Gist options
  • Save tienthanh2509/846973cd80f13c231caaaa38303d40c6 to your computer and use it in GitHub Desktop.
Save tienthanh2509/846973cd80f13c231caaaa38303d40c6 to your computer and use it in GitHub Desktop.
Ubuntu VirtualBox Service for virtual machine
#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides: VirtualBox Run Machine
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d. This example start a
# single forking daemon capable of writing a pid
# file. To get other behavoirs, implemend
# do_start(), do_stop() or other functions to
# override the defaults in /lib/init/init-d-script.
### END INIT INFO
# Author: Phạm Tiến Thành <phamthanh@d13ht01.tk>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.
. /lib/lsb/init-functions
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
VB_USER=thanh
VB_MACHINE="598a00b2-2394-482a-a9dc-4995335e2b6e"
getStatus() {
runuser -l $VB_USER -c "VBoxManage showvminfo ${VB_MACHINE} | grep 'State:'"
}
case "$1" in
start)
log_daemon_msg "Starting Machine ${VB_MACHINE}" "vb-iteee" || true
runuser -l $VB_USER -c "VBoxHeadless --startvm ${VB_MACHINE}" &
getStatus
;;
stop)
log_daemon_msg "Stopping Machine ${VB_MACHINE}" "vb-iteee" || true
runuser -l $VB_USER -c "VBoxManage controlvm ${VB_MACHINE} acpipowerbutton"
getStatus
;;
force-stop)
runuser -l $VB_USER -c "VBoxManage controlvm ${VB_MACHINE} poweroff"
getStatus
;;
restart)
runuser -l $VB_USER -c "VBoxManage controlvm ${VB_MACHINE} reset"
getStatus
;;
status)
getStatus
;;
*)
log_action_msg "Usage: /etc/init.d/virtualbox-iteee {start|stop|force-stop|restart|status}" || true
exit 1
esac
exit 0
@tienthanh2509
Copy link
Author

Need change VB_USER and VB_MACHINE

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