1

This is the first time I use launchd so there´s a lot I don´t know.

A little bit of background info.

I set up everyting to be able to use the mail command to send emails with my gmail. Wrote a bash script (IPSend.sh) using ifconfig, grep, and mail. Running the script manually as any user including root works perfectly and sends the email to the specified email address. Copied the script to /usr/local/bin/ the directories have rwxr-xr-x permissions and the script -rwxr-xr-x@ everything is root:wheel. Created a plist file and copied it to /Library/LaunchDaemons (-rw-r--r--@ 1 root wheel 918 Dec 25 16:01 com.fer.ip.plist), debugged it with plutil -lint until result was OK.

When I launchctl load /Library/LaunchDaemon/com.fer.ip.plist (as root) I don´t get any errors the email is not sent and the log shows something like:

Dec 25 16:18:00 dbe4b6a01 com.apple.launchd[1] ("com.fer.ip"): Throttling respawn: Will start in 6 seconds
Dec 25 16:18:06 dbe4b6a01 com.apple.launchd[1] ("com.fer.ip"): Throttling respawn: Will start in 10 seconds
Dec 25 16:18:37: --- last message repeated 2 times --- Dec 25 16:18:37 dbe4b6a01 com.apple.launchd[1] ("com.fer.ip"): Throttling respawn: Will start in 10 seconds
Dec 25 16:18:37 dbe4b6a01 com.apple.launchd[1] (0x7f9170659600.anonymous.sendmail[20303]): Failed to add kevent for PID 20303. Will unload at MIG return
Dec 25 16:18:37 dbe4b6a01 com.apple.launchd[1] (0x7f9170659600.anonymous.sendmail[20303]): Unloading PID 20303 at MIG return.
Dec 25 16:18:47 dbe4b6a01 com.apple.launchd[1] ("com.fer.ip"): Throttling respawn: Will start in 10 seconds Dec 25 16:19:00: --- last message repeated 1 time ---
Dec 25 16:19:00 dbe4b6a01 com.apple.launchd[1] ("com.fer.ip"): Throttling respawn: Will start in 7 seconds

The reason I say "something like this" is that somtimes I dont get the "Failed to add kevent for PID" or it appears earlier.

I'm totally lost. No idea what the "Failed to add kevent for PID" means or why it is not working as a launchd process when it works manually. Please help!

Here's my script changing my email address:

#!/bin/sh

ifconfig | grep -o -E [0-9]+[.][0-9]+[.][0-9]+[.][0-9]+ |grep -v "127" |grep -v "255" | mail -s "iMac" [email protected]

and here's my plist file:

<?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.fer.ip"</string>

        <key>ProgramArguments</key>

        <array> 
            <string>/usr/local/bin/IPSend.sh</string>
        </array>

        <key>OnDemand</key>
        <false/>

        <key>StartInterval</key>
        <integer>60</integer>

        <key>KeepAlive</key>
                <true/>

        <key>StandardOutPath</key>
        <string>/tmp/test.stdout</string>

        <key>StandardErrorPath</key>
        <string>/tmp/test.stderr</string>

</dict>
</plist>

Both standard error and standard out files are always empty

1
  • Instead of piping to grep three times, just use: ifconfig | awk '/broadcast/{print $2}' Aug 27, 2017 at 13:03

2 Answers 2

1

I don't know why I missed these the first time, but upon reviewing your problem just now, I noticed a few inconsistencies in the plist file which might well be the problem. I think this will fix your problem.

    <?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>com.fer.ip</string>
                            <key>Program</key>
                                <string>/usr/local/bin/IPSend.sh</string>
                            <key>RunAtLoad</key>
                                <true/>
                            <key>KeepAlive</key>
                                <false/>    
                   </dict> 
</plist>

Hope that this helps!

F.

0

A few suggestions:

  1. In the plist, change:

    OnDemand to true; KeepAlive to false
    
  2. Redesign the shell script to report status to standard out or fail and report to standard error. Like this:

    #!/bin/bash
    
    MYSTDOUT=/tmp/test.stdout
    
    MYSTDERR=/tmp/test.stderr
    
    [[ ifconfig | grep -o -E [0-9]+[.][0-9]+[.][0-9]+[.][0-9]+ | grep -v "127" | grep -v "255" | mail -s "iMac" [email protected] ]] && [[ echo "(X) SUCCESS: email sent" > ${MYSTDOUT} ; exit [0] ]]
    
    echo "(!) FAILURE: email not sent" 2> ${MYSTDERR} 
    
    exit [1]
    

Hope that this helps.

1
  • Thank you very much for your suggestions.I will look into this and let you know the results.
    – FerOZ
    Aug 9, 2017 at 8:36

You must log in to answer this question.

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