我想知道是否有某种方法可以在应用程序打开时停止启动任务,然后在应用程序关闭时再次启动它。 我的 launchd 任务设置为在文件更改时收到通知,然后对该文件执行一些 UNIX 代码。 但是,我的应用程序对此文件进行了大量更改,因此当我的应用程序打开时,我无法运行该任务(否则每次更改文件时它将运行 UNIX 代码,这不好)。 执行此操作的不同方法有优点和缺点吗(即使我还没有找到任何方法)?
谢谢你的帮助。
I'm wondering if there is some way I can stop a launchd task when a application is open, and then start it again when the application is closed. My launchd task is set to be notified when a file is changed and then do some UNIX code with the file. However, my application makes a lot of changes to this file so I can't have the task running when my app is open (or else it will run the UNIX code every time that the file is changed, which isn't good). Are there pros and cons to the different methods to do this (even though I haven't found any methods)?
Thanks for any help.
发布评论
评论(3)
如果您喜欢冒险,您可以尝试 launchd 自己的 API,它位于 /usr/include/launch.h 中。 查看 launchd_stop_job 的实现="nofollow noreferrer">launchctl.cpp。
If you're feeling adventurous, you might try launchd's own API, which is in /usr/include/launch.h. Check out the implementation of
launchd_stop_job
in launchctl.cpp.您可以使用 applescript 来检查应用程序是否正在运行。
我发现这篇文章描述了一个将监视应用程序启动和关闭的 applescript: http://macosx .com/forums/1199085-post2.html
我确信一个熟练的
bash
脚本编写者可以告诉您一种方法,通过解析top
的输出来完成同样的事情>。Apple 文档
do shell 脚本
You could use applescript to check and see if an app is running.
I found this post that describes an applescript that will monitor an application's startup and shutdown: http://macosx.com/forums/1199085-post2.html
I am sure an accomplished
bash
scripter could tell you a way to do the same thing by parsing the output fromtop
.Apple documentation for
do shell script
请参阅 launchd.plist 手册页以了解 KeepAlive 和 PathState 关键字。
Please consult the launchd.plist man page for the KeepAlive and PathState keywords.