6

I've got a plist set to run at a set interval, and have put it in /Library/LaunchDaemons like so:

/Library/LaunchDaemons $ ll macports_update_notifier.plist 
lrwxr-xr-x  1 root  wheel    55B  5 Sep 13:47 macports_update_notifier.plist@ -> /Users/seron/bin/macports/macports_update_notifier.plist

The problem is that it doesn't seem to execute. Here's the plist:

<?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>EnvironmentVariables</key>
    <dict>
        <key>HOME</key>
        <string>/Users/seron</string>
    </dict>
    <key>Label</key>
    <string>macports_update_notifier</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/seron/bin/macports/macports_notificaton.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>300</integer>
</dict>
</plist>

I loaded it with sudo launchctl load -w /Library/LaunchDaemons/macports_update_notifier.plist.

Here's what sudo launchctl list macports_update_notifier produces:

{
    "Label" = "macports_update_notifier";
    "LimitLoadToSessionType" = "System";
    "OnDemand" = true;
    "LastExitStatus" = 0;
    "TimeOut" = 30;
    "ProgramArguments" = (
        "/Users/seron/bin/macports/macports_notificaton.sh";
    );
};

The script executes in about a second and works fine when run on its own. It needs to run as root.

I'm using OS X 10.8.1. What could be the problem?

edit:

My script runs terminal-notifier which is command line tool to display notifications in Mountain Lion and this is where the daemon bails.

#!/bin/sh
/opt/local/bin/terminal-notifier -message 'output' -group 'debug'

I found that the daemon runs fine if it's not loaded with sudo. One difference is that launchctl load test.plist reports "LimitLoadToSessionType" = "Aqua" instead of "System". However if I set LimitLoadToSessionType to Aqua in the plist and load with sudo launchctl refuses to load it displaying nothing found to load. It needs to run as root however because it updates the macports database.

update

I think I've found the reason why a GUI can't be launched by a daemon; Mac Developer Library, Daemons and Agents. An agent can do that. In my case, it needs to communicate with the daemon in order to display information whenever the daemon has updated macports. Now the question is, How do I make the agent ask and the daemon answer?

6
  • Yes it is. It has the same flags/ownership as the symlink.
    – ivvi
    Commented Sep 5, 2012 at 13:13
  • Maybe terminal-notifier doesn't work if running under a user different from the one currently logged in. Does calling terminal-notifier with sudo in an interactive shell work ok?
    – nohillside
    Commented Sep 6, 2012 at 8:59
  • @patrix it does
    – ivvi
    Commented Sep 6, 2012 at 9:07
  • And you call terminal-notifier with the full path in your script?
    – nohillside
    Commented Sep 6, 2012 at 9:29
  • @patrix I added the script to my question.
    – ivvi
    Commented Sep 6, 2012 at 10:27

2 Answers 2

5

You can send to Notification Centre using Applescript (osascript )in you shell script.

There is a very good thread here that explains the various ways of either running only Applescript as a shell script or being able to mix applescript in with bash, for example.

I created a shell script with:

#!/bin/bash
theDate=`date '+DATE: %m/%d/%y TIME:%H:%M:%S'`

osascript -e "display notification \"$theDate\" with title \"test 1\" subtitle \" sub 1\""

I then created a LaunchDaemon in the same way you have and loaded it without sudo.

<?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>notifTest</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/userName/Documents/notify.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>90</integer>
</dict>
</plist>

Every 90 seconds I get a notification:

enter image description here

The notification icon you will notice is the Script Editors one. Which means the Notification is controlled by the Script Editors Notification settings in Notification Centre

0
0

Try:

<key>ProgramArguments</key>
<array>
    <string>/bin/sh</string>
    <string>/Users/seron/bin/macports/macports_notificaton.sh</string>
</array>

so /bin/sh /your/script.sh

tested this and works:

lrwxr-xr-x   1 root  wheel    25  5 sep 15:48 macports.update.notifier.plist -> /Users/clt/bin/test.plist

content:

<?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>EnvironmentVariables</key>
    <dict>
        <key>HOME</key>
        <string>/Users/clt</string>
    </dict>
    <key>Label</key>
    <string>macports.update.notifier</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>/Users/clt/bin/test.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>30</integer>
</dict>
</plist>

the test.plist and the test.sh

-rw-r--r--  1 root  staff  555  5 sep 15:52 /Users/clt/bin/test.plist
-rwxr-xr-x  1 root  wheel   40  5 sep 15:45 /Users/clt/bin/test.sh

the content of test.sh

sh-3.2# cat ~clt/bin/test.sh
#!/bin/sh
echo "works" >> /tmp/test.txt
6
  • Nothing happens here. No output to the system.log either.
    – ivvi
    Commented Sep 5, 2012 at 15:10
  • Strange, because the above works on ML (at least on my notebook). Try exactly copy everything to your system, change only "/Users/clt" to your...
    – clt60
    Commented Sep 5, 2012 at 15:35
  • maybe the problem is in the naming... i changed your underscore_named.plist to usual dot.named.plist.
    – clt60
    Commented Sep 5, 2012 at 15:41
  • I made a test just likes yours and it works. Now I have to find out why my doesn't.
    – ivvi
    Commented Sep 5, 2012 at 21:16
  • My script runs terminal-notifier link and this makes the script fail and after that point the daemon stops working too. I've experimented by loading it as a user without sudo, i.e. launchctl load test.plist and in that case all works fine. launchctl list testreports "LimitLoadToSessionType" = "Aqua" whereas if loaded with sudo it's set to "System". If I try to set it to Aqua in the plist and load with sudo I get nothing found to load. The script needs to run as root because it updates the macports database.
    – ivvi
    Commented Sep 6, 2012 at 8:21

You must log in to answer this question.

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