I want to run some application in background and later kill it by pid.
pipe = IO.popen("firefox 'some_url' 2>&1 &")
pipe.pid
This code starts firefox and return me some pid, but unfortunately it's not firefox's pid.
pipe = IO.popen("firefox")
pipe.pid
This code starts firefox and return mi some pid, firefox's pid. Is there any solution to start external application and get its pid? Firefox is only for example it could be any other application. I also tried with libs like: Open3 and Open4 but seems to be the same effect. I also wonder if '$!' bash variable is good solution for this? Run something in background and read '$!', what do you think?
Since you are running it in the background (
command &
), you get the interpreter's PID:Drop the
&
:For the additional issue with the redirection, see @kares' answer
it's not just about running it in the background but also due
2>&1
redirecting err/out causes
IO.popen
to put another process in front of your actual process (pipe.pid
won't be correct)here's a detailed insight: http://unethicalblogger.com/2011/11/12/popen-can-suck-it.html
possible fix for this could be using
exec
e.g.IO.popen("exec firefox 'some_url' 2>&1")