Ok you see six jobs. I get all the PIDs there, for example, the last
job has the PID 7242.
I can then kill this process like so:
kill -9 7242
But from within a .rb script this does not seem to work.
I need to get the PID of the last executed program somehow, but
only those started from an instance of bash (I embed my bash into
konsole tabs, they all have different jobs of course)
system ‘jobs -l’
Does not work, neither does
result = jobs -l
command not found: jobs -l
It fails because jobs is a bash internal command.
Is there any other way I can find out the PIDs of
the jobs that were started from that bash instance?
Is there any other way I can find out the PIDs of
the jobs that were started from that bash instance?
why not just use plain old ps? if you have the PID of the shell in
question, you can use that (ps --pid PID); or, if your script is
an immediate child of the target shell, you can use its PPID (ps --ppid #{Process.ppid}). combined with output specifier (’–format
pid’) and sort option (’–sort start_time’) you can extract the last
started job’s PID (excluding Process.pid, of course).
But from within a .rb script this does not seem to work.
I need to get the PID of the last executed program somehow, but
only those started from an instance of bash (I embed my bash into
konsole tabs, they all have different jobs of course)
system ‘jobs -l’
Does not work, neither does
Obviously because you are not asking the shell which started
background processes but you create a new shell. This is the same as
doing this on a bash prompt (last two commands):
Is there any other way I can find out the PIDs of
the jobs that were started from that bash instance?
What is it that you are trying to achieve? One obvious way would be
to pass the information from bash to the Ruby script. Another,
probably much better, option would be to start your background
processes from the Ruby script (fork & exec, IO.popen etc.).
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.