Professional Web Applications Themes

Run a process in the background - PERL Beginners

Hi Perlers, I know that questions like this get asked all the time, but I guess it's just my turn to ask 'em! I need to kick of some processes in my script. However, the script needs to kick them all off at once and then stick around to do some other things. I'm kinda new to Perl, but in my OS's shell, I'd do this (actually, I DO do this ... this Perl script will be replacing this shell script): #!/bin/sh nohup /path/to/process/one/bin/server.sh >/dev/null 2>&1 & nohup /path/to/process/two/bin/server.sh >/dev/null 2>&1 & nohup /path/to/process/three/bin/server.sh >/dev/null 2>&1 & I figure I ...

  1. #1

    Default Run a process in the background

    Hi Perlers,

    I know that questions like this get asked all the time, but I guess
    it's just my turn to ask 'em!

    I need to kick of some processes in my script. However, the script
    needs to kick them all off at once and then stick around to do some
    other things. I'm kinda new to Perl, but in my OS's shell, I'd do
    this (actually, I DO do this ... this Perl script will be replacing
    this shell script):

    #!/bin/sh
    nohup /path/to/process/one/bin/server.sh >/dev/null 2>&1 &
    nohup /path/to/process/two/bin/server.sh >/dev/null 2>&1 &
    nohup /path/to/process/three/bin/server.sh >/dev/null 2>&1 &

    I figure I can pass that string directly to system() in Perl,

    system "nohup /path/to/process/one/bin/server.sh >/dev/null 2>&1 &";
    system "nohup /path/to/process/two/bin/server.sh >/dev/null 2>&1 &";
    system "nohup /path/to/process/three/bin/server.sh >/dev/null 2>&1 &";

    but that invokes the shell, right? Also, I'd like to capture the
    Process ID of those 3 servers.

    So I tried the above and it works, but I'd still like to be able to
    leave the shell out of this. And what about those process IDs?

    One last question. If I do the above, does the OS consider those 3
    servers my scripts "children"? I know that comes with some
    responsibility (I've been looking at some things about the SIGCHLD
    signals). oh, and btw, this is Solaris I'm talking about here.

    Thanks guys and gals,

    --Errin
    Errin Guest

  2. #2

    Default Re: Run a process in the background

    > Hi Perlers, 

    Shell is considerably different than Perl when it comes to this stuff...
     

    A good figure, though I would expect this to fail, since system blocks,
    but maybe not because the shell should be returning control.
     

    Yes it does.
     

    In this case you should look at the fork/exec model instead. It allows
    you to start a separate process without blocking the parent which you
    can then use to do other things while the forked process is running. The
    best place to start is with the IPC (InterProcess Communication) docs under,

    perldoc perlipc
    perldoc -f fork
    perldoc -f exec
    perldoc -f wait
    perldoc -f waitpid

    This will cover forking/execing, signals, etc.
     

    Sort of, in the case of C<system> the function manages the stuff you are
    talking about automatically itself. In the lower level you will have to
    manage it, but the docs listed above should provide enough info to get
    you through that.
     

    Per usual this is where I will jump in and suggest POE if you are doing
    anything that is not trivial. It makes managing all of this type of
    multi-tasking garbage simple.

    http://poe.perl.org

    HTH,

    http://danconia.org

    Wiggins Guest

  3. #3

    Default Re: Run a process in the background

    Errin Ln wrote: 

    Hello,
     

    The perlipc man page has a lot of information on how to run a child process
    and how to capture/ignore signals like HUP.

    perldoc perlipc


    This might do what you want (UNTESTED!)

    my children;
    for my $server ( </path/to/process/*/bin/server.sh> ) {
    local $SIG{ HUP } = 'IGNORE';
    defined( my $pid = fork ) or die "Cannot fork: $!";
    unless ( $pid ) { # in child
    open STDOUT, '>', '/dev/null' or die $!;
    open STDERR, '>', '/dev/null' or die $!;

    exec $server or die "Cannot exec $server: $!";
    }
    push children, $pid;
    }



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. Listener background process
    By theInvestor in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: February 13th, 06:56 PM
  2. exec background process
    By Matt in forum PHP Development
    Replies: 1
    Last Post: September 11th, 12:01 PM
  3. Background process
    By Jackson Miller in forum PHP Development
    Replies: 1
    Last Post: August 14th, 06:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139