启动守护进程不会从 Bash 脚本加载,但会从终端加载



我在安装后的Bash脚本文件中有这一行,它从PKG安装程序中运行:

launchctl load /Library/LaunchDaemons/com.mycompany.myapp.plist

PKG正确安装作业列表文件和作业调用的applet。

但是当我检查作业是否在终端中加载时,我没有得到任何返回:

launchctl list | grep mycompany

如果我在Terminal中执行相同的load命令,作业将按预期加载。

为什么当脚本运行时作业没有被加载?

当您以普通用户身份运行launchctl list时,它会列出在您的用户会话中运行的LaunchAgents;要列出LaunchDaemons,以root身份运行它(即sudo launchctl list)。

更多细节:一些launchctl子命令允许您显式指定要处理的域。例如:

launchctl print system      # Prints Launch *Daemons*
launchctl print user/501    # Prints Launch *Agents* for user #501's session

但更老的"遗产"子命令,如listloadunloadstartstop等早于此约定,并使用命令运行的用户ID来确定要操作的域。例如:

launchctl load /path/to/plist         # Loads the plist as an Agent in my user session
launchctl list                        # Lists Agents in my user session
sudo launchctl load /path/to/plist    # Loads it as a Daemon in the system domain
sudo launchctl list                   # Lists Daemons in the system domain

您的包可能以root身份运行脚本,因此它将作为守护进程加载作业(这是您想要的),但这可能取决于包的配置方式。

相关内容

最新更新