打开应用程序时停止启动任务

发布于 2024-07-24 07:01:17 字数 226 浏览 9 评论 0 原文

我想知道是否有某种方法可以在应用程序打开时停止启动任务,然后在应用程序关闭时再次启动它。 我的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泼猴你往哪里跑 2024-07-31 07:01:17

如果您喜欢冒险,您可以尝试 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.

暮倦 2024-07-31 07:01:17

您可以使用 applescript 来检查应用程序是否正在运行。

我发现这篇文章描述了一个将监视应用程序启动和关闭的 applescript: http://macosx .com/forums/1199085-post2.html

global wasLoaded

on run
    set wasLoaded to isAppLoaded("Safari")

    idle
end run

on idle
    set x to isAppLoaded("Safari")
    if x and not wasLoaded then
        do shell script "SOME BASH COMMAND" -- stop your launchd task
        set wasLoaded to true
    else if wasLoaded and not x then
        do shell script "SOME BASH COMMAND" -- start your launchd task
        set wasLoaded to false
    end if
    return 1 --will wait 1 second before checking again
end idle

on isAppLoaded(app_name)
    tell application "System Events"
        set app_list to every application process whose name contains app_name
        if the (count of app_list) > 0 then
            return true
        else
            return false
        end if
    end tell
end isAppLoaded

我确信一个熟练的 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

global wasLoaded

on run
    set wasLoaded to isAppLoaded("Safari")

    idle
end run

on idle
    set x to isAppLoaded("Safari")
    if x and not wasLoaded then
        do shell script "SOME BASH COMMAND" -- stop your launchd task
        set wasLoaded to true
    else if wasLoaded and not x then
        do shell script "SOME BASH COMMAND" -- start your launchd task
        set wasLoaded to false
    end if
    return 1 --will wait 1 second before checking again
end idle

on isAppLoaded(app_name)
    tell application "System Events"
        set app_list to every application process whose name contains app_name
        if the (count of app_list) > 0 then
            return true
        else
            return false
        end if
    end tell
end isAppLoaded

I am sure an accomplished bash scripter could tell you a way to do the same thing by parsing the output from top.

Apple documentation for do shell script

沩ん囻菔务 2024-07-31 07:01:17

请参阅 launchd.plist 手册页以了解 KeepAlive 和 PathState 关键字。

Please consult the launchd.plist man page for the KeepAlive and PathState keywords.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文