Bash 如何创建可以访问$HOME变量的macOS启动作业

Bash 如何创建可以访问$HOME变量的macOS启动作业,bash,macos,launchd,Bash,Macos,Launchd,我有一个PKG安装程序,它安装一个启动的作业,如下所示: launchctl bootstrap gui/501 ~/Library/LaunchAgents/com.mycompany.myproduct.plist 文件如下: /Library/LaunchDaemons/com.mycompany.myproduct.plist 安装后脚本按如下方式激活它: launchctl bootstrap gui/501 ~/Library/LaunchAgents/com.mycompany.

我有一个PKG安装程序,它安装一个启动的作业,如下所示:

launchctl bootstrap gui/501 ~/Library/LaunchAgents/com.mycompany.myproduct.plist
文件如下:

/Library/LaunchDaemons/com.mycompany.myproduct.plist

安装后脚本按如下方式激活它:

launchctl bootstrap gui/501 ~/Library/LaunchAgents/com.mycompany.myproduct.plist
launchctl load/Library/LaunchDaemons/com.mycompany.myproduct.plist

作业运行一个小程序,该小程序需要为登录用户获取$HOME变量。当从Finder手动运行时,它会按预期工作,但从launchd运行时,$HOME变量返回空字符串

我尝试将工作转移到这两个位置的代理,结果相同:

/库/启动代理
~/Library/launchagent

为什么它不工作?如何访问$HOME变量

2021年4月8日更新

在使用旧的/不推荐使用的load命令时,目标域似乎存在一些挑战。因此,我重写了load命令,如下所示:

launchctl bootstrap gui/501 ~/Library/LaunchAgents/com.mycompany.myproduct.plist
这显然是假设机器只有一个用户,所以它不是很健壮

这是经过消毒的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.mycompany.myproduct.agent</string>
    <key>LimitLoadToSessionType</key>
    <string>Aqua</string>
    <key>Program</key>
        <string>/Library/Application Support/mycompany/myproduct/agent.app/Contents/MacOS/applet</string>
    <key>StandardErrorPath</key>
    <string>/dev/null</string>
    <key>StandardOutPath</key>
    <string>/dev/null</string>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>12</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

标签
com.mycompany.myproduct.agent
LimitLoadToSessionType
水绿色
节目
/库/应用程序支持/mycompany/myproduct/agent.app/Contents/MacOS/applet
标准错误路径
/dev/null
标准门诊
/dev/null
StartCalendarInterval
时辰
12
分钟
0

然后,agent.app需要为活动用户获取$HOME变量。

“当前登录的用户”?如果有很多用户登录怎么办?真的!这就是为什么我尝试将作业plist移动到~/Library/LaunchAgents,但在那里也不起作用。如果显示
plist
文件,会更容易提供帮助。请参阅上面的更新@MarkSetchellYou可能会在apple.stackexchange.com上获得更好的运气。问题是
launchd
由root用户运行,而不是由拥有~/Library/LaunchAgents的用户运行。您需要通过
launchd
本身而不是通过环境来获得适当的用户。