Absent somebody pointing out a way to do this within Parallels itself, here's a solution using only native utilities - thanks @user3439894 for suggesting a Launch Agent!
- Create a
/usr/local/bin/parallels_vm_starting
shell script and chmod 755
it to make it executable:
#!/bin/bash
(
echo "----------"
date
if /usr/bin/tty -s
then
echo "interactive"
else
# sleep for a bit to give things a chance to settle
echo "background - let things settle"
sleep 15
date
fi
# Actions to perform when hosted VM starts up
/usr/local/Homebrew/bin/brew services restart redis
# etc. as needed
date
) 2>&1 | tee -a /tmp/vm-startup.log
- Create a Launch Agent file
~/Library/LaunchAgents/parallels_vm_startup.plist
with this content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>parallels_vm_startup</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/parallels_vm_starting</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/Parallels/parallels_dhcp_leases</string>
</array>
<key>StandardOutPath</key>
<string>/dev/null</string>
</dict>
</plist>
This will monitor the file /Library/Preferences/Parallels/parallels_dhcp_leases
, which Parallels modifies when the VM starts (assuming the VM has networking enabled), and will execute the shell script when that happens.
Note: the DHCP leases file is regularly updated while the VM is up. If you only want to react to a VM being launched or shut down, monitor the file /Library/Preferences/Parallels/vmdirectorylist.desktop.xml
instead.
- Activate the Agent:
launchctl load ~/Library/LaunchAgents/parallels_vm_startup.plist