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:
if /usr/bin/tty -s
# sleep for a bit to give things a chance to settle
echo "background - let things settle"
# Actions to perform when hosted VM starts up
/usr/local/Homebrew/bin/brew services restart redis
# etc. as needed
) 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">
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
- Activate the Agent:
launchctl load ~/Library/LaunchAgents/parallels_vm_startup.plist