how to kill firefox gracefully from command line

2019-07-11 18:05发布

I am writing a program that needs to open a list of URLs. I have firefox on Ubuntu12.04, and every time I kill firefox and reopen it, there will be a message asking if I want to restore the previous sessions or not, which is quite annoying.

I invoke firefox(in Java) using

Runtime r= Runtime.getRuntime();
r.exec("firefox -new-instance www.google.com");

and kill it using

r.exec("killall firefox");

Is there a way to kill and reopen firefox more gracefully? I want the program run automatically without any pop-up messages or human intervention. Thanks a lot!

3条回答
Juvenile、少年°
2楼-- · 2019-07-11 18:37

Instead of launching firefox directly, launch a shell script that launches firefox in the background, then prints the PID of the firefox process on the standard out.

Then you can read the PID back through the Process object returned by Runtime.exec()

查看更多
女痞
3楼-- · 2019-07-11 18:51

I think you cannot kill Firefox gracefully from Java, but you can change your firefox settings so that it wont ask you everytime it re-opens.

See the link Firefox Restore Options which will help you.

查看更多
Luminary・发光体
4楼-- · 2019-07-11 18:52

Use WebDriver to load and stop Firefox. You can get a separate Firefox instance with a separate profile. You won't be asked to restore sessions.

FirefoxProfile fp = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(fp);
driver.get(url);

Then to close the browser and any child windows:

driver.quit();
查看更多
登录 后发表回答