OSX why my launchagent process starting as root instead of current user

4.5k views Asked by At

I want to run the process as the current user. But it is starting as root. How can I start the process as the current user?

This script is run in postinstall in a OSX package. Packaging using pkgbuild.

Is there any relation to how I build the command line tools in Xcode?

NAME="myapp"
SCPROXY_INSTALLATION_DIR="/opt/local/bin"

# Script identifier (same as package identifier).
IDENTIFIER="com.mycomp.myapp"

LAUNCH_AGENT_PLIST="/Library/LaunchAgents/$IDENTIFIER.plist"

# Write LaunchDaemon plist file.
echo '<?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>'$IDENTIFIER'</string>
<key>ProgramArguments</key>
<array>
<string>'$SCPROXY_INSTALLATION_DIR/$NAME'</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/var/log/scproxy.log</string>
<key>StandardErrorPath</key>
<string>/var/log/scproxy.log</string>
<key>Debug</key>
<true/>

</dict>
</plist>' > "$LAUNCH_AGENT_PLIST"

/bin/launchctl unload  $LAUNCH_AGENT_PLIST
/bin/launchctl load  $LAUNCH_AGENT_PLIST

#exit 0
# Check LaunchDaemon is loaded.
STATUS=`/bin/launchctl list | /usr/bin/grep $IDENTIFIER | /usr/bin/awk '{print $3}'`

if [ "$STATUS" = "$IDENTIFIER" ]
then
echo "Success: LaunchAgent loaded."
exit 0
else
echo "Error: LaunchAgent not loaded."
exit 1
fi
2

There are 2 answers

2
Manikandaraj Srinivasan On BEST ANSWER

You are adding Launch Agent plist to System level launch agents directory, it will start the App wit root/admin privileges only. You need to place the launchagents in USER Level Launch Agents directory, which USER_HOME/Library/LaunchAgents/ , ex : /Users/mani/Library/LaunchAgents/.

Also if you gonna run the app as current user, then the log paths are invalid, since current user won't have privileges to write log in /var/log. So you need to change the log directory as well.

Also load and unload from launchctl as current user, not as admin/root user.

https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

0
Truong Kevin On

That because you put your *plist in LaunhDaemons. All *.plist in LaunchDeamon will run in ROOT. You need to put your *.plist in LaunchAgent. it's can be /Library/LaunchAgents or System/Library/LaunchAgents