简体   繁体   中英

How to check for exit of mainstream web browsers?

This is the situation: I would like to open a browser window from python code, as in any of the following (portability is not an issue atm):

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

I can unfortunately not just check if the pid I created is still alive, as most browser (Firefox, Chrome) will immediately exit the creating process and spawn more (ie one per tab) as they want.

What is the most responsible (doesn't kill other browser windows) and portable (works at least with firefox and chrome under Linux) of achieving this?

Edit : Can ppids be used in this context?

You can "ping" firefox process. There is a part of my open_url shell script below I hope you find it useful

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM