From: Robert Klemme Date: 2012-04-10T21:22:59+09:00 Subject: Re: Bash - "jobs -l" command from ruby. HMmmmm. On Tue, Apr 10, 2012 at 12:22 PM, Marc Heiler wrote: > 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 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. :( > > 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/