I have a plist in the /Library/LaunchAgent folder (this should run as "logged in user", if I understand correctly but it seems to be running as root). It launches a .sh bash script.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
<plist version="1.0">

The script, for troubleshooting, contains the following lines:

curUser=$(id -u -n)
osascript -e 'tell app "Finder" to display dialog "'$curUser'"' 


osascript -e 'tell app "Finder" to display dialog "'$curUser'"' 

The message box result is always:


This means that any scripting that I am trying to do has issues. Can anyone shed any light on why this is happening. The script runs on each login, runs before the user's desktop appears (the message appears on the login screen).

Curiously, if I use the following code:

osascript -e 'tell app "Finder" to display dialog "'$1'"' 

It returns the correct username. It seems that the script is running as root, but has passed the parameter of the logged in user (note that the plist has no parameters).

Running Mavericks 10.9.5

  • What happens if it runs from: /Users/$USER/Library/LaunchAgents/ Jun 18, 2015 at 16:27
  • Looks like you are feeding it to the system launchd using sudo. How do you load the agent?
    – LCC
    Jun 18, 2015 at 16:39
  • 1) I can't put in in the /Users/$USER area as it may not exist yet (network login). 2) LCC: I'm simply putting the plist into the /Library/LaunchAgent folder and it launches.
    – MrBeatnik
    Jun 18, 2015 at 16:59
  • 1
    Did you accidentally created another job in /Library/LaunchDaemons or /System/Library/LaunchDaemons. Please move the .plist to another location (away from /Library/LaunchAgents) and reboot your machine. Does the job start again?
    – LCC
    Jun 19, 2015 at 6:50
  • 1
    Agents started from /Library/LaunchAgent can't run as "logged in user", because there may be none or several users logged in when the agent is started.
    – nohillside
    Jun 19, 2015 at 9:35

2 Answers 2


A LoginHook was present. Loginhooks run as root and was calling the same file. The following command removed the hook:

sudo defaults delete com.apple.loginwindow LoginHook

Thank you all for your time, and apologies for the wild goose chase. I knew it wasn't correct.


From the launchd.plist man:

UserName < string> This optional key specifies the user to run the job as. This key is only applicable when launchd is running as root.

So You can add this key to Your plist to specify the User.

  • 1
    Handy key to know for others, but wont work for me as it must run in the context of any user that logs in.
    – MrBeatnik
    Jun 19, 2015 at 10:04

You must log in to answer this question.

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