OSX 为什么我的 launchagent 进程以 root 而不是当前用户启动

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

我想运行进程作为当前用户。但它是以 root 身份启动的。我如何以当前用户身份启动进程?

此脚本 运行 在 OSX 软件包的安装后。使用 pkgbuild 打包。

我在 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 }'`

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

您正在将 Launch Agent plist 添加到系统级启动代理目录,它将仅以 root/admin 权限启动应用程序。您需要将 launchagents 放在 USER Level Launch Agents 目录中,即 USER_HOME/Library/LaunchAgents/ ,例如:/Users/mani/Library/LaunchAgents/

此外,如果您要 运行 应用程序作为当前用户,则日志路径无效,因为当前用户将没有权限写入登录 /var/log。所以你也需要更改日志目录。

还以当前用户身份从 launchctl 加载和卸载,而不是 admin/root 用户。

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

那是因为您将 *plist 放在了 LaunhDaemons 中。 LaunchDeamon 中的所有 *.plist 都将在 ROOT 中 运行。 您需要将您的 *.plist 放入 LaunchAgent。它可以是 /Library/LaunchAgents 或系统/Library/LaunchAgents