Script to auto-start and auto-stop virtual machines on mac

スポンサーリンク
スポンサーリンク

At first

Virtual machines built on macOS can be treated as terminated if the host macOS is shut down while the virtual machine is running, and the files of the virtual machine may be corrupted.

It is a script to start and stop automatically by linking the virtual machine to macOS to prevent this.

The script you are using is published as vm-configure on github, so please download it from github or download it from here.

See the link below for the vm-configure folder configuration.

VirtualBox and Vagrant are required to run the script.If it is not installed, please refer to the link below.

/host-osx/ubuntu-20.04/com.collbow.vagrant.plist

On macOS, you can use launchd to run scripts on a regular basis.The com.collbow.vagrant.plist file is an XML-format configuration file paired with key tags and settings, set the configuration to run the script, and register it with launchd using the launchctl command.

To register with launchd, see /host-osx/ubuntu-20.04/vm-create.command.

KeySetting valueContents
Labelcom.collbow.vagrantSet the distinguished name.
ProgramArguments<array>
<string>sh</string>
<string>[SCRIPT_DIR]/com.collbow.vagrant.sh</string>
</array>
Set the programs and parameters that you want to run.
[SCRIPT_DIR]is replaced by the script execution directory with /host-osx/ubuntu-20.04/vm-create.command and registered in launchd.
RunAtLoadTrueIf true, the program set to ProgramArguments will run when the setting is loaded.
ExitTimeOut60Set the amount of time to wait when launchd is trying to stop the script.
If the time is short, it will be terminated before the virtual machine ends.
WorkingDirectory[SCRIPT_DIR]Set up a working directory.
[SCRIPT_DIR]is replaced by the script execution directory with /host-osx/ubuntu-20.04/vm-create.command and registered in launchd.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.collbow.vagrant</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>[SCRIPT_DIR]/com.collbow.vagrant.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ExitTimeOut</key>
    <integer>60</integer>
    <key>WorkingDirectory</key>
    <string>[SCRIPT_DIR]</string>
  </dict>
</plist>

/host-osx/ubuntu-20.04/com.collbow.vagrant.sh

This is a script that runs when macOS starts and stops by launchd.

The location of the script file is in the current directory.

SCRIPT_DIR=$(dirname "$0")
cd "$SCRIPT_DIR"

The execution result of the script is set to output to the file (com.collbow.vagrant.sh.log) with .log after the execution script file name, and the execution start date and time are output to the log file.

LOG_FILE="${0%.*}.log"
date "+%Y/%m/%d %H:%M:%S" >> $LOG_FILE

The script executed by launchd has set path because there is no PATH in /usr/local/bin, /usr/bin.

Also, if a bash initialization file (.bash_profile) exists, the .bash_profile is loading the following environment variables:

export PATH=$PATH:/usr/local/bin:/usr/bin
if [ -e "${HOME}/.bash_profile" ]; then
    . "${HOME}/.bash_profile"
fi

Defines a callback function that is called when the script execution process needs to be stopped.

This callback function is called when the script is stopped (for example, by shutting down macOS), logs the date and time when the stop process started, loads the Vagrantfile in the current directory in vagrant halt, and stops the virtual machine defined in the Vagrantfile.

If the virtual machine can be stopped successfully in osascript, a notification message is displayed.

onShutdown() {
    date "+%Y/%m/%d %H:%M:%S" > $LOG_FILE
    echo "Collbow VMs is Stopping." >> $LOG_FILE
    vagrant halt >> $LOG_FILE
    echo "Collbow VMs is stopped." >> $LOG_FILE
    osascript -e 'display notification "Collbow VMs is stopped." with title "Collbow VMs"'
    exit
}

vagrant up is loading the Vagrantfile in the current directory and starting the virtual machine defined in the Vagrantfile.

If the virtual machine was successfully started in osascript, a notification message is displayed.

echo "Collbow VMs is starting." >> $LOG_FILE
vagrant up >> $LOG_FILE
echo "Collbow VMs is started." >> $LOG_FILE
osascript -e 'display notification "Collbow VMs is started." with title "Collbow VMs"'

Trapping signals from macOS.

Run onShutdown when a sIGUP, SIGINT, SIGQUIT, or SIGTERM signal is sent from macOS.

trap 'onShutdown' SIGHUP SIGINT SIGQUIT SIGTERM

This is an infinite loop to prevent the script from terminating until the script execution process is stopped due to macOS shutdown, etc.

while true; Do (<a0
    sleep 86400 & wait
Done

Comment

スポンサーリンク