laurentj / slimerjs

A scriptable browser like PhantomJS, based on Firefox
http://slimerjs.org
Other
3k stars 258 forks source link

Bash script should kill firefox subprocess when killed (trap SIGTERM) #587

Open tbroyer opened 7 years ago

tbroyer commented 7 years ago

versions

Steps to reproduce the issue

  1. Run some script through SlimerJS that's long enough so you have time to do the following steps in parallel; something like the following should do it:
    setTimeout(function() {
      require('system').stdout.write("Finished after 10 seconds.");
      phantom.exit(0);
    }, 10000);
  2. Kill the SlimerJS process (bash script). If the SlimerJS process was run in the background in the same terminal, use kill $!, otherwise killall slimerjs should do it.
  3. Look for running processes: ps aux |grep slimer

Actual results:

The Firefox child process has been orphaned. It is listed by ps and will print its output to the console upon termination.

Expected results:

The Firefox child process is killed.

jaekunchoi commented 4 years ago

Is there any update to this? I have lots of FireFox child processes running in the background and not killed because of this issue. Any fix suggestions would be great

survtur commented 3 years ago

I start slimerjs from python script and i met the same problem. I found interesting solution at https://stackoverflow.com/questions/39420683/killing-a-sub-subprocess-from-python-bash-without-leaving-orphans

# python3

import os
import signal

# start process
process = subprocess.Popen(slimer_command ..., preexec_fn=os.setpgrp)
pid = process.pid

# kill process
os.killpg(os.getpgid(pid), signal.SIGTERM)

Description of os functions: https://docs.python.org/3/library/os.html. I am sure there is enough info to implement the same in bash script.

The idea is that you perform setpgrp before starting slimerjs and then kill its process group.