process ids

Discussion in 'Perl Misc' started by Robert Mohr, Jan 15, 2004.

  1. Robert Mohr

    Robert Mohr Guest

    Is there a way for a program outside of Perl to be run in the background
    and have Perl store the process id. That is, if I want to run the
    program 'program', I could run something similar to `program &` and get
    something like '[1] 12834' from it (what I'd get from running 'program
    &' from the command line).
    --
    Robert Mohr

    -state.edu
    Robert Mohr, Jan 15, 2004
    #1
    1. Advertising

  2. Robert Mohr

    Ben Morrow Guest

    Robert Mohr <> wrote:
    > Is there a way for a program outside of Perl to be run in the background
    > and have Perl store the process id. That is, if I want to run the
    > program 'program', I could run something similar to `program &` and get
    > something like '[1] 12834' from it (what I'd get from running 'program
    > &' from the command line).


    (untested)

    use POSIX qw/:sys_wait_h/;

    $SIG{CHLD} = sub { 1 while 0 < waitpid -1, WNOHANG };

    my $pid = fork;
    defined $pid or die "can't fork: $!";
    $pid or exec "program";

    Ben

    --
    The cosmos, at best, is like a rubbish heap scattered at random.
    - Heraclitus
    Ben Morrow, Jan 15, 2004
    #2
    1. Advertising

  3. Robert Mohr

    Robert Mohr Guest

    Ben Morrow wrote:
    > Robert Mohr <> wrote:
    >
    >>Is there a way for a program outside of Perl to be run in the background
    >>and have Perl store the process id. That is, if I want to run the
    >>program 'program', I could run something similar to `program &` and get
    >>something like '[1] 12834' from it (what I'd get from running 'program
    >>&' from the command line).

    >
    > (untested)
    >
    > use POSIX qw/:sys_wait_h/;
    >
    > $SIG{CHLD} = sub { 1 while 0 < waitpid -1, WNOHANG };
    >
    > my $pid = fork;
    > defined $pid or die "can't fork: $!";
    > $pid or exec "program";


    Thank you. One more question though: how do you kill the process from
    the id? Can it be done through Perl (I assume it can, but can't find
    out how from perldoc POSIX, which is rather confusing) or must I do it
    through sh?
    --
    Robert Mohr

    -state.edu
    Robert Mohr, Jan 15, 2004
    #3
  4. Robert Mohr

    Paul Lalli Guest

    On Thu, 15 Jan 2004, Robert Mohr wrote:

    > Thank you. One more question though: how do you kill the process from
    > the id? Can it be done through Perl (I assume it can, but can't find
    > out how from perldoc POSIX, which is rather confusing) or must I do it
    > through sh?


    perldoc -f kill


    Paul Lalli
    Paul Lalli, Jan 15, 2004
    #4
  5. Robert Mohr

    Robert Mohr Guest

    Paul Lalli wrote:
    > On Thu, 15 Jan 2004, Robert Mohr wrote:
    >
    >
    >>Thank you. One more question though: how do you kill the process from
    >>the id? Can it be done through Perl (I assume it can, but can't find
    >>out how from perldoc POSIX, which is rather confusing) or must I do it
    >>through sh?

    >
    >
    > perldoc -f kill


    Guess that makes sense. Thank you.
    --
    Robert Mohr

    -state.edu
    Robert Mohr, Jan 16, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Tolman
    Replies:
    0
    Views:
    2,959
    Jeff Tolman
    Nov 4, 2003
  2. Richard Nunez
    Replies:
    0
    Views:
    275
    Richard Nunez
    Jan 28, 2004
  3. Replies:
    2
    Views:
    694
    Beat Bolli
    Jun 21, 2005
  4. herman
    Replies:
    5
    Views:
    306
    Karthik Gurusamy
    Sep 5, 2007
  5. TJ
    Replies:
    1
    Views:
    123
    Mike Moore [MSFT]
    Oct 3, 2003
Loading...

Share This Page