#/bin/bash -e export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/Library/Application Support/VMware Fusion" VMNAME="$1" VMX="/Virtual Machines/${VMNAME}.vmwarevm/${VMNAME}.vmx" if [ ! -f "$VMX" ] then echo "$VMX" not found >&2 exit 1 fi function wait_for_host() { local WAIT=180 while ! [ -e /var/run/vmnet-bridge-vmnet.pid -o -e /var/run/vmnet-bridge-vmnet0.pid ]; do local WAIT=$((WAIT - 1)) if [ $WAIT -le 0 ] then echo Timeout waiting for VMware drivers. exit 1 fi sleep 1 done while ! who | awk '{print $2}' | grep -q ^console$ do local WAIT=$((WAIT - 1)) if [ $WAIT -le 0 ] then echo Timeout waiting for console login. exit 1 fi sleep 1 done } function is_running() { local VMX="$1" ps -eco pid=,command= | awk '{if ($2=="vmware-vmx") print $1}' | while read PID do if ps -o args= $PID | tr ' ' '\n' | grep -qFx "$VMX" then return 42 #found fi done [ "$?" == "42" ] } wait_for_host if is_running "$VMX" then echo "$1" already running. Attaching to existing. else vmrun start "$VMX" nogui fi trap 'vmrun stop "$VMX" soft' SIGKILL SIGTERM SIGHUP SIGINT while is_running "$VMX" do sleep 5 done