0

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

1 Answer 1

1

When a launchd-managed process (like your script) exits, launchd will "clean up" (i.e. kill) any leftover subprocesses. I'm not sure of the details, but apparently that can include something that /usr/bin/mail fires off in the background to send the message. In my experience, launchd will usually kill it before it finishes sending the message. Solution: tell launchd not to kill leftover subprocesses by adding this to the launch daemon plist:

<key>AbandonProcessGroup</key>
<true/>
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .