如何在 MacOS 上加载系统范围的守护进程
我正在尝试使用此命令加载我的守护程序应用程序 launchctl load /Library/LaunchDaemons/myPlistFileName
它工作正常,但仅当我的用户登录系统时。
我如何加载我的守护程序应用程序,它将在没有任何用户登录系统(如 Windows 服务)的情况下保持加载状态?
命令 sudo launchctl load /Library/LaunchDaemons/myPlistFileName 给我一个错误 -> 没有找到要加载的内容
哪种方法是正确的?
编辑:我的 Plist 文件
<key>Label</key>
<string>com.myCompany.myApplication</string>
<key>ProgramArguments</key>
<array>
<string>open</string>
<string>-g</string>
<string>/Applications/myAppDir/myApplication.app</string>
</array>
<key>UserName</key>
<string>root</string>
<key>GroupName</key>
<string>wheel</string>
<key>KeepAlive</key>
<true/>
<key>OnDemand</key>
<false/>
目前是这样:在系统启动时,我的守护进程以 root 权限启动。当我使用我的用户帐户登录后,守护进程将重新启动,并且我的用户是该进程的新所有者
I'm trying to load my daemon-app with this command launchctl load /Library/LaunchDaemons/myPlistFileName
It works fine, but only while my user is logged in on system.
How can i load my deamon-app, which will stay loaded without any user logged into the system (like the windows-services)??
The command sudo launchctl load /Library/LaunchDaemons/myPlistFileName
give me an error -> nothing found to load
Which is the right way to do this?
EDIT: My Plist-File
<key>Label</key>
<string>com.myCompany.myApplication</string>
<key>ProgramArguments</key>
<array>
<string>open</string>
<string>-g</string>
<string>/Applications/myAppDir/myApplication.app</string>
</array>
<key>UserName</key>
<string>root</string>
<key>GroupName</key>
<string>wheel</string>
<key>KeepAlive</key>
<true/>
<key>OnDemand</key>
<false/>
Currently it is so: at system-start my daemon starts with root-permissions. After i've logged in with my user-account, the daemon-process restarts and my user is the new owner of the process
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用“sudo launchctl load; sudo launchctl start”或者,在较新版本的 launchctl 上,“sudo launchctl Submit -l <label> --[...]”是可行的方法。
在没有实际看到 PLIST 的情况下,很难诊断为什么你的“sudo launchctl”命令失败,但是你应该确保 PLIST 的权限对每个人都是可读的(但只能由 root:wheel 或 root:admin 写入),你应该确保已给出“UserName”和“GroupName”字段,您应该确保“Program”给出了相关程序的绝对路径(例如,不依赖于用户的 PATH 的特定设置)环境变量),并且应正确定义“WorkingDirectory”和“EnvironmentVariables”字段(如果适用)。
另请参阅: man launchd。 plist
编辑:
在尝试执行“sudo ... load”之前,您还可以尝试运行“stop”和“unload”命令。
编辑:
现在您已经上传了 PLIST 文件,很清楚您的问题是什么......以其他用户身份运行时无法使用 open 命令。请参阅以 root 身份运行 OS X GUI 应用程序。另外,我很确定,KeepAlive 和 OnDemand 密钥是免费的。我认为这些可以删除。
Using "sudo launchctl load <name-of-plist-file>; sudo launchctl start <job-name>" or, on newer versions of launchctl, "sudo launchctl submit -l <label> -- <command> [<arg0> ... <argN>]" is the way to go.
It is hard to diagnose why your "sudo launchctl" command failed without actually seeing the PLIST, but you should make sure that the permissions for the PLIST are readable for everyone (but writeable only by root:wheel or root:admin), you should be sure that the "UserName" and "GroupName" fields have been given, you should make sure that "Program" gives an absolute path to the program in question (and does not, for example, rely on the user's particular setting of the PATH environment variable), and the "WorkingDirectory" and "EnvironmentVariables" fields should be properly defined if applicable.
See Also: man launchd.plist
EDIT:
You might also try running the "stop" and "unload" commands before attempting to do the "sudo ... load".
EDIT:
Now that you've uploaded your PLIST file it is clear what your problem is... you cannot use the open command when running as another user. See running OS X GUI app as root. Also, the KeepAlive and OnDemand keys are, I'm pretty sure, gratuitous. I think those can be deleted.