[Pdx-pm] job control

Michael G Schwern schwern at pobox.com
Mon Jun 2 03:30:42 CDT 2003


On Sun, Jun 01, 2003 at 06:15:33PM -0700, Randall Hansen wrote:
> I'm trying to get a list of bash's suspended jobs using Perl.  I
> initially thought this would be trivial (e.g. perl -e "print exec
> jobs"), but I've yet to succeed.  I tried permutations of "system,"
> "exec," backticks, etc.  The closest I've come is:	$ perl -e "print
> `jobs`" which apparently does an eval on the output of jobs (causing
> errors and yielding nothing useful).  But what I really want is to
> capture and examine the output, not echo it.
>
> It occurs to me (after seeing "No such file ..." a few times) that
> technically jobs is a builtin of bash, not a system command.  This
> hasn't gotten me any closer.  I can start a new bash with perl, but
> can't figure out how to send builtins to an existing one.
> 
> Which in turn makes me think that this may be more of a bash question
> than a Perl one, but I thought I'd ask anyway.

`bash -c jobs` will "work" but not do what you want since it will
report its own child processes, not the children of your parent process.

Think of it like this...

- your shell
    - background_program_1
    - background_program_2
    - perl
        - bash

When "your shell" asks for its jobs it gets its children, the two
backgrounded programs and perl.  When perl runs bash and asks for its jobs,
it gets nothing because that newly spawned bash process has no children.

To do what you want you probably want to use pstree to get information
about the children of the perl process's parent (ie. your shell's kids) or
use Proc::ProcessTable.


-- 
Remember, any tool can be the right tool.
    -- Red Green



More information about the Pdx-pm-list mailing list