【问题标题】:How to check for exit of mainstream web browsers?如何检查主流浏览器是否退出?
【发布时间】:2012-09-03 11:07:44
【问题描述】:

情况是这样的:我想从 python 代码打开一个浏览器窗口,如下所示(可移植性不是问题):

subprocess.call(['xdg-open', 'file:///someFileIGenerated.html'])
... # or
pid = subprocess.Popen(...)
... # or (from the webbrowser package)
webbrowser.open(...)

不幸的是,我不能只检查我创建的 pid 是否仍然存在,因为大多数浏览器(Firefox、Chrome)会立即退出创建过程并根据需要生成更多(即每个标签一个)。

实现这一点最负责任(不会杀死其他浏览器窗口)和可移植性(至少在 Linux 下与 firefox 和 chrome 一起使用)是什么?

编辑:可以在这种情况下使用 ppid 吗?

【问题讨论】:

    标签: google-chrome firefox fork subprocess pid


    【解决方案1】:

    您可以“ping”firefox 进程。下面是我的 open_url shell 脚本的一部分,希望对你有用

    OUTPUT=`firefox -remote "ping()" 2>&1 | grep No`
    if [[ "${OUTPUT}" == "Error: No running window found" ]]; then
        # firefox is not running
    else
        # firefox still running;
    fi
    

    【讨论】:

    • 这将检查任何 Firefox 窗口,对吗?因此,如果用户在我的代码生成新窗口之前打开了 firefox,我无法检测到他是否只关闭了我的窗口。我明白你的意思,但这需要我在创建窗口之前和之后运行的 pid 之间设置差异。不太舒服或不安全。
    猜你喜欢
    • 2012-02-11
    • 2023-03-14
    • 2013-05-29
    • 2020-01-04
    • 2019-12-30
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多