是否“launchctl unload”?在 OSX 上传递“ProgramArguments”到目标应用程序?
我正在尝试设置一个 .plist
文件以与 OSX 上的 launchctl
一起使用。当从命令行运行时(不使用launchctl),我们的应用程序执行为:
/path/to/ourapp
...并终止我们的应用程序,我们键入:
/path/to/ourapp -k
...这会导致ourapp的新实例
正确终止正在运行的前一个实例。
现在,我设置了一个 .plist
文件来通过 launchctl
控制应用程序的执行,如下所示:
// start.plist
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>ourapp</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/ourapp</string>
</array>
<key>OnDemand</key>
<false/>
<key>UserName</key>
<string>daniel347x</string>
<key>SHAuthorizationRight</key>
<string>system.preferences</string>
</dict>
... 我在命令行执行它,如下所示
launchctl load /path/to/start.plist
:成功启动应用程序。
不幸的是,在创建 stop.plist
时,如下所示(唯一的区别是添加了 -k
参数):
// stop.plist
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>ourapp</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/ourapp</string>
<string>-k</string> // <-- only difference is adding this argument
</array>
<key>OnDemand</key>
<false/>
<key>UserName</key>
<string>daniel347x</string>
<key>SHAuthorizationRight</key>
<string>system.preferences</string>
</dict>
... 并通过
launchctl unload /path/to/stop.plist
... 应用程序 执行不会终止...因此,似乎在使用 launchctl
时,应用程序的执行并不等同于
/path/to/ourapp -k
有人能告诉我 launchctl unload
真正在做什么 - 即是它使用给定的参数从命令行调用应用程序,或者不使用 - 并且在使用 launchctl unload
时,无论它在做什么,我该怎么做才能让我的 stop.plist
正常工作?
I am attempting to set up a .plist
file for use with launchctl
on OSX. When run from the command line (not using launchctl
), our application is executed as:
/path/to/ourapp
... and to terminate our application, we type:
/path/to/ourapp -k
... which causes the new instance of ourapp
to properly kill the previous instance that is running.
Now I set up a .plist
file to control execution of the application via launchctl
, as follows:
// start.plist
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>ourapp</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/ourapp</string>
</array>
<key>OnDemand</key>
<false/>
<key>UserName</key>
<string>daniel347x</string>
<key>SHAuthorizationRight</key>
<string>system.preferences</string>
</dict>
... and I execute it at the command line, as follows:
launchctl load /path/to/start.plist
This works successfully to launch the application.
Unfortunately, when creating a stop.plist
, as follows (the only difference is the addition of the -k
argument):
// stop.plist
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>ourapp</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/ourapp</string>
<string>-k</string> // <-- only difference is adding this argument
</array>
<key>OnDemand</key>
<false/>
<key>UserName</key>
<string>daniel347x</string>
<key>SHAuthorizationRight</key>
<string>system.preferences</string>
</dict>
... and executing via
launchctl unload /path/to/stop.plist
... the application does NOT terminate ... therefore, it seems that when using launchctl
, the application is not being executed equivalently to
/path/to/ourapp -k
Can someone tell me what launchctl unload
is really doing - i.e. is it calling the application from the command line with the given arguments, or not - and what I can do to get my stop.plist
to work when using launchctl unload
, given whatever it is doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
高级别
否,OSX 上的“launchctl unload”不会将“ProgramArguments”传递给目标应用程序。它从 launchd 正在运行的 plist 中删除 plist。
简短的回答
我不确定您是否按照设计的方式使用 launchctl 和 launchd,但请尝试:
较长的回答
launchctl 命令用于控制 launchd 在任何给定时间运行哪些 plist。当您发出:
plist 被加载到 launchd 中并且您的“ourapp”被触发。关键的一点是,除非“ourapp”卸载start.plist,否则它仍然会在launchd中运行。您可以通过查看以下输出来检查这一点:
您调用卸载 stop.plist 没有任何效果的原因是它从未加载到 launchd 中。为了能够停止它,您必须首先启动它:
这样做的技巧是,一旦您执行此操作,您的 start.plist 和 stop.plist 都将运行。显然,我还没有测试您的应用程序/设置,但这可能会导致奇怪的行为。另外,我不确定在 start.plist 上再次运行“load”会做什么,因为它已经在运行了。
根据您在此处的描述,launchd 和 launchctl 看起来可能不是最好的方法。如果你想坚持下去,我认为你需要将系统调用添加到你的“ourapp”中,这样当它发出kill命令时,它也会卸载启动和停止plist。或者,编写一个代理脚本来卸载 plist,然后调用您的“ourapp -k”。这样,下次您使用“launchctl load /path/to/start.plist”时,您将不会尝试启动已经在运行的东西。
另一张纸条。您可能刚刚使用“ourapp”作为占位符,但值得指出的是您可能想要使用唯一的标签名称。如果没有别的事,它将帮助您使用“launchctl list”来跟踪它们。我不知道 launchd 本身是否会存在重复名称的问题,但避免这种可能性似乎是最安全的。
High level
No, "launchctl unload" on OSX does not pass "ProgramArguments" to the target application. It removes the plist from the ones that launchd is running.
Short answer
I'm not sure you are using launchctl and launchd the way it was designed, but try:
Longer answer
The launchctl command is used to control which plists launchd runs at any given time. When you issue:
the plist is loaded into launchd and your "ourapp" is trigged. A critical point is that unless "ourapp" unloads the start.plist, it will still be running in launchd. You can check this by viewing the output from:
The reason your call to unload the stop.plist didn't have any effect is that it was never loaded into launchd to begin with. To be able to stop it, you must first start it with:
The trick with this is that once you do this, both your start.plist and stop.plist will be running. Obviously, I haven't tested your app/setup, but this could cause weird behavior. Also, I'm not sure what running the "load" again on your start.plist would do since it would already be running.
Form what you've described here, it looks like launchd and launchctl might not be the best approach. If you want to stick with it, I think you would need to add system calls into your "ourapp" so that when it gets issued the kill command it also unloads both the start and the stop plist. Or, write a proxy script that unloads the plists and then calls your "ourapp -k". That way, the next time you use "launchctl load /path/to/start.plist" you won't be trying to start something that's already running.
One other note. You might have just been using the "ourapp" as place holders, but it's worth pointing out that you probably want to use unique Label names. If nothing else, it'll help you keep track of them with "launchctl list". I don't know if launchd itself would have an issue with duplicate names, but it seems safest to avoid the possibility.