Bash - "jobs -l" command from ruby. HMmmmm

Hi,

My situation:

In bash, I start jobs and tasks etc…

These all get a PID.

Now with the command:

jobs -l

I can list these and get their PIDs.

This command gives me a list like this (example):

[1] 24056 Stopped (signal) tt++ /tmptintin/run.tin (wd: /)
[2] 24816 Stopped (signal) tt++ /tmptintin/run.tin (wd:
/depot/RUBY/TOOLS)
[3] 7660 Stopped (signal) tt++ /tmptintin/run.tin (wd: /)
[4] 28912 Stopped (signal) tt++ /tmptintin/run.tin (wd:
/depot/RUBY/test)
[5]- 21064 Stopped (signal) tt++ /tmptintin/run.tin (wd: /)
[6]+ 7242 Stopped (signal) tt++ /tmptintin/run.tin (wd:
/depot/RUBY/foo)

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. :frowning:

Is there any other way I can find out the PIDs of
the jobs that were started from that bash instance?

hi marc!

Marc H. [2012-04-10 12:22]:

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).

hope that helps…

cheers
jens

On Tue, Apr 10, 2012 at 12:22 PM, Marc H. [email protected]
wrote:

jobs -l
/depot/RUBY/test)

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):

$ sleep 120 &
[1] 4256
$ jobs -l
[1]+ 4256 Running sleep 120 &
$ ( jobs -l )
$ bash -c ‘jobs -l’
$

result = jobs -l

command not found: jobs -l

It fails because jobs is a bash internal command. :frowning:

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