On my macOS 10.13 system, I have a bash script launched as a LaunchDaemon (in /Library/LaunchDaemons) every minute to check whether an Ubuntu virtual machine is running in VMware Fusion. (In the event of a power outage or something, I have to log in as my self to get the VM running again.) I have Postfix set up to relay to my email provider. My script works fine when run in a foreground process, either as my regular user or as root, resulting in emailing or not emailing me as appropriate, but when run by launchd, it just will not send me mail. I've tailed /var/log/mail.log and see nothing other than the usual daemon start and exit messages. I've also verified that the return status of the mail command is 0 by echoing it to a log file. Any suggestions as what's going on, or how to debug further?
My plist in /Library/LaunchDaemons:
<?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.planetexpress.checkvmware</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/checkvmware</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
My script:
#!/bin/bash
touch /tmp/checkvmware
checkline="/path/to/vm.vmx"
vmwareline=$(pgrep -if "$checkline")
if [[ -z $vmwareline ]]; then
/usr/bin/mail -s "Alert: Ubuntu on VMware isn't running" [email protected] <<< 'Are you even logged in, bro?'
mystatus=$?
echo "Status of mail command: $mystatus" >> /tmp/checkvmware.log
fi