Why does the LaunchAgent return ROOT as the user

launchd

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"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.user.uloginscript</string>
        <key>Program</key>
        <string>/Library/TestArea/netcall.sh</string>
        </dict>
</plist>

The script, for troubleshooting, contains the following lines:

#!/bin/bash
curUser=$(id -u -n)
osascript -e 'tell app "Finder" to display dialog "'$curUser'"' 

OR

#!/bin/bash
curUser=$(USER)
osascript -e 'tell app "Finder" to display dialog "'$curUser'"' 

The message box result is always:

root

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:

#!/bin/bash
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

Best Answer

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.