DockUtil doesn't work when run through LaunchAgent

lilshim
New Contributor

My problem is very straight forward: I'm trying to run commands for every user that logs in through using a LaunchAgent that runs a bash script:

#!/bin/bash
#/usr/bin/testing.sh

mkdir -p /Users/myname/Created


/usr/local/bin/dockutil --add "/Users/myname/Networks"
sudo dockutil --add "/usr/bin"
sudo dockutil --add "/usr/"
sudo dockutil --list > /Users/myname/Desktop/test.txt

In /Library/LaunchAgents/com.myname.test.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>Label</key>
        <string>com.sait.test.app</string>
        <key>ProgramArguments</key>
        <array>
                <string>/bin/sh</string>
                <string>/usr/bin/testing.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardOutPath</key>
        <string>/var/log/myjob.log</string>
        <key>Debug</key>
        <true/>

    </dict>
    </plist>

The folder is created on login. But, none of the DockUtil commands are executed. That is, my dock isn't changed and the test.txt doesn't contain any text. When I run these manually in the terminal, they work fine.

Why is this? Is there something that is wrong with how dock-util is loaded, or how it executes that I'm not aware of?

5 REPLIES 5

mm2270
Legendary Contributor III

2 things to try

  1. Put the full path to the dockutil binary in the script wherever its being called, so use /usr/local/bin/dockutil for each command, instead of just dockutil Also, lose the sudo because LaunchAgents can't do anything with sudo; they run as the user, which is what you want here. Dockutil doesn't require root level commands to work.

  2. Put a --no-restart at the end of each dockutil line to prevent it from trying to restart the dock after each item is added in. I've found that the OS does not take kindly to the Dock being restarted several times in a row so quickly, so its possible the OS is stepping in and just stopping the script from continuing.

For the last dockutil command, you can leave out the --no-restart and it will restart the dock after it has added in all the items, but, you may also need to add in a killall cfprefsd for good measure, I've seen success in getting the Dock to populate correctly with dockutil scripts when adding that in.

Edit: Last thing, try enclosing any paths in single quotes. For some reason dockutil seems to prefer that, and since you aren't putting any variables in there that would not expand correctly, it should be OK. But try the other items first, as it may not be necessary to change the quoting.

stevewood
Honored Contributor II
Honored Contributor II

I just tested this in a VM and the following worked for me:

#!/bin/bash
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
myUser="/Users/${loggedInUser}"

mkdir -p $myUser/Created

/private/var/inte/bin/dockutil --add "/Users/integer/Networks" $myUser
/private/var/inte/bin/dockutil --add "/usr/bin" $myUser
/private/var/inte/bin/dockutil --list > /Users/integer/Desktop/test.txt $myUser

Look
Valued Contributor III

All of the above and a

sleep 30

at the start of the script.
I found especially on 10.11 that if you run dockutil immediately on login some process within OS X prevents the results from showing on the dock, give OS X long enough to settle down first and then it works fine.

calumhunter
Valued Contributor

You should move your script out of

/usr/bin

you wont be able to do that in 10.11

/usr/local/bin

is as good a place as any

sean
Valued Contributor

You have also created a bash script and then are using sh to call the script. I'd just ditch that line and use the Program key instead of the ProgramArguments array.

Have you checked the users dock file to see if the items have actually been added, but just not represented graphically on the dock?