system call

Discussion in 'Perl Misc' started by hendedav@gmail.com, Jun 5, 2007.

  1. Guest

    Gang,

    I am using a cgi to call a pl script and then pass xml back to
    the web browser (code listed below). The problem I am having is that
    the code in the cgi executes promptly and as it should, but the return
    values, for some reason, don't get sent to the browser until the pl
    script is completed. Currently the pl script accepts two parameters
    (but does nothing with the first one currently) and just uses a while
    loop that counts to the 2nd passed value. If anyone can help with
    this (or knows a better way to get the pid, please let me know.

    Dave



    $_ = param('runnow'); # passed variable value
    if (system("./backup.pl \"$_\" 30 \&") == 0) {
    $_ = `ps aux|grep "backup.pl $_" 2>&1`; # finds the pid of the
    pl
    my ($pid) = /(\d{3,})/; # isolates the pid only
    chomp($pid);
    my @info = stat("/tmp/buj$pid"); # this file is created by the pl
    script

    print "Content-type: text/xml\n\n\n";
    print "<info pid=\"". $pid ."\" date=\"". scalar(localtime) ."\" /
    >";

    }
     
    , Jun 5, 2007
    #1
    1. Advertising

  2. Guest

    On Jun 5, 2:57 pm, Jim Gibson <> wrote:
    > In article <>,
    >
    >
    >
    > <> wrote:
    > > Gang,

    >
    > > I am using a cgi to call a pl script and then pass xml back to
    > > the web browser (code listed below). The problem I am having is that
    > > the code in the cgi executes promptly and as it should, but the return
    > > values, for some reason, don't get sent to the browser until the pl
    > > script is completed. Currently the pl script accepts two parameters
    > > (but does nothing with the first one currently) and just uses a while
    > > loop that counts to the 2nd passed value. If anyone can help with
    > > this (or knows a better way to get the pid, please let me know.

    >
    > > Dave

    >
    > > $_ = param('runnow'); # passed variable value
    > > if (system("./backup.pl \"$_\" 30 \&") == 0) {
    > > $_ = `ps aux|grep "backup.pl $_" 2>&1`; # finds the pid of the
    > > pl
    > > my ($pid) = /(\d{3,})/; # isolates the pid only
    > > chomp($pid);

    >
    > Unnecessary, as $pid will contain only decimal digits.
    >
    > > my @info = stat("/tmp/buj$pid"); # this file is created by the pl
    > > script

    >
    > > print "Content-type: text/xml\n\n\n";
    > > print "<info pid=\"". $pid ."\" date=\"". scalar(localtime) ."\" /
    > > >";

    > > }

    >
    > Where is the while loop you described? Are you describing one program
    > and posting another?
    >
    > In any case, what happens after your Perl program emits output is up to
    > your web server and the client's browser. It is not under Perl's
    > control. However, you can maybe help things to move along more quickly
    > by flushing the output. See 'perldoc -q flush' for details.
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com



    Thanks for the reply. The while loop is stored in the pl file (listed
    below). But if perl sends the xml listed above and exists immediately
    after, why would the browser not process it? Since the system call
    has an appended & at the end to indicate to run as a seperate process,
    the browser shouldn't be waiting on perl to finish that additional
    script call, right?



    entire contents of the test.pl script:
    ----------------------------------------
    #!/usr/bin/perl -w

    my $test = $ARGV[0];
    my $duration = $ARGV[1];

    $_ = `ps aux|grep "test.pl $test" 2>&1`;
    my ($pid) = /(\d{3,})/;
    chomp($pid);
    open(TMP, ">/tmp/buj$pid") || die "Can't create: $!\n";
    close(TMP);

    my $i=0;
    while ($i<$duration) {
    sleep(1);
    $i++;
    }
    unlink "/tmp/buj$pid"; # erase the /tmp/bujPID file
     
    , Jun 5, 2007
    #2
    1. Advertising

  3. Guest

    The original section of code was from the cgi script that calls the
    test.pl script. Sorry for not specifying.

    Dave
     
    , Jun 5, 2007
    #3
  4. Guest

    On Jun 5, 3:11 pm, wrote:
    > On Jun 5, 2:57 pm, Jim Gibson <> wrote:
    >
    >
    >
    > > In article <>,

    >
    > > <> wrote:
    > > > Gang,

    >
    > > > I am using a cgi to call a pl script and then pass xml back to
    > > > the web browser (code listed below). The problem I am having is that
    > > > the code in the cgi executes promptly and as it should, but the return
    > > > values, for some reason, don't get sent to the browser until the pl
    > > > script is completed. Currently the pl script accepts two parameters
    > > > (but does nothing with the first one currently) and just uses a while
    > > > loop that counts to the 2nd passed value. If anyone can help with
    > > > this (or knows a better way to get the pid, please let me know.

    >
    > > > Dave

    >
    > > > $_ = param('runnow'); # passed variable value
    > > > if (system("./backup.pl \"$_\" 30 \&") == 0) {
    > > > $_ = `ps aux|grep "backup.pl $_" 2>&1`; # finds the pid of the
    > > > pl
    > > > my ($pid) = /(\d{3,})/; # isolates the pid only
    > > > chomp($pid);

    >
    > > Unnecessary, as $pid will contain only decimal digits.

    >
    > > > my @info = stat("/tmp/buj$pid"); # this file is created by the pl
    > > > script

    >
    > > > print "Content-type: text/xml\n\n\n";
    > > > print "<info pid=\"". $pid ."\" date=\"". scalar(localtime) ."\" /
    > > > >";
    > > > }

    >
    > > Where is the while loop you described? Are you describing one program
    > > and posting another?

    >
    > > In any case, what happens after your Perl program emits output is up to
    > > your web server and the client's browser. It is not under Perl's
    > > control. However, you can maybe help things to move along more quickly
    > > by flushing the output. See 'perldoc -q flush' for details.

    >
    > > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > > ----------------------------------------------------------
    > > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > > ----------------------------------------------------------
    > > http://www.usenet.com

    >
    > Thanks for the reply. The while loop is stored in the pl file (listed
    > below). But if perl sends the xml listed above and exists immediately
    > after, why would the browser not process it? Since the system call
    > has an appended & at the end to indicate to run as a seperate process,
    > the browser shouldn't be waiting on perl to finish that additional
    > script call, right?
    >
    > entire contents of the test.pl script:
    > ----------------------------------------
    > #!/usr/bin/perl -w
    >
    > my $test = $ARGV[0];
    > my $duration = $ARGV[1];
    >
    > $_ = `ps aux|grep "test.pl $test" 2>&1`;
    > my ($pid) = /(\d{3,})/;
    > chomp($pid);
    > open(TMP, ">/tmp/buj$pid") || die "Can't create: $!\n";
    > close(TMP);
    >
    > my $i=0;
    > while ($i<$duration) {
    > sleep(1);
    > $i++;}
    >
    > unlink "/tmp/buj$pid"; # erase the /tmp/bujPID file




    Can anyone else offer any advice or solutions to this problem? Or
    does anyone know a better way to find the pid of a running perl
    program?

    Thanks,
    Dave
     
    , Jun 6, 2007
    #4
  5. wrote:
    > Or
    > does anyone know a better way to find the pid of a running perl
    > program?


    perldoc perlvar:
    $$ The process number of the Perl running this script. You should
    consider this variable read-only, although it will be altered
    across fork() calls. (Mnemonic: same as shells.)

    jue
     
    Jürgen Exner, Jun 6, 2007
    #5
  6. Guest

    On Jun 6, 8:58 am, "Jürgen Exner" <> wrote:
    > wrote:
    > > Or
    > > does anyone know a better way to find the pid of a running perl
    > > program?

    >
    > perldoc perlvar:
    > $$ The process number of the Perl running this script. You should
    > consider this variable read-only, although it will be altered
    > across fork() calls. (Mnemonic: same as shells.)
    >
    > jue



    Thanks Jue! Thats much simplier!
     
    , Jun 6, 2007
    #6
  7. Mumia W. Guest

    On 06/06/2007 07:30 AM, wrote:
    > On Jun 5, 3:11 pm, wrote:
    >> On Jun 5, 2:57 pm, Jim Gibson <> wrote:
    >>
    >>
    >>
    >>> In article <>,
    >>> <> wrote:
    >>>> Gang,
    >>>> I am using a cgi to call a pl script and then pass xml back to
    >>>> the web browser (code listed below). The problem I am having is that
    >>>> the code in the cgi executes promptly and as it should, but the return
    >>>> values, for some reason, don't get sent to the browser until the pl
    >>>> script is completed. Currently the pl script accepts two parameters
    >>>> (but does nothing with the first one currently) and just uses a while
    >>>> loop that counts to the 2nd passed value. If anyone can help with
    >>>> this (or knows a better way to get the pid, please let me know.
    >>>> Dave
    >>>> $_ = param('runnow'); # passed variable value
    >>>> if (system("./backup.pl \"$_\" 30 \&") == 0) {
    >>>> $_ = `ps aux|grep "backup.pl $_" 2>&1`; # finds the pid of the
    >>>> pl
    >>>> my ($pid) = /(\d{3,})/; # isolates the pid only
    >>>> chomp($pid);
    >>> Unnecessary, as $pid will contain only decimal digits.
    >>>> my @info = stat("/tmp/buj$pid"); # this file is created by the pl
    >>>> script
    >>>> print "Content-type: text/xml\n\n\n";
    >>>> print "<info pid=\"". $pid ."\" date=\"". scalar(localtime) ."\" /
    >>>>> ";
    >>>> }
    >>> Where is the while loop you described? Are you describing one program
    >>> and posting another?
    >>> In any case, what happens after your Perl program emits output is up to
    >>> your web server and the client's browser. It is not under Perl's
    >>> control. However, you can maybe help things to move along more quickly
    >>> by flushing the output. See 'perldoc -q flush' for details.
    >>> Posted Via Usenet.com Premium Usenet Newsgroup Services
    >>> ----------------------------------------------------------
    >>> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    >>> ----------------------------------------------------------
    >>> http://www.usenet.com

    >> Thanks for the reply. The while loop is stored in the pl file (listed
    >> below). But if perl sends the xml listed above and exists immediately
    >> after, why would the browser not process it? Since the system call
    >> has an appended & at the end to indicate to run as a seperate process,
    >> the browser shouldn't be waiting on perl to finish that additional
    >> script call, right?
    >>
    >> entire contents of the test.pl script:
    >> ----------------------------------------
    >> #!/usr/bin/perl -w
    >>
    >> my $test = $ARGV[0];
    >> my $duration = $ARGV[1];
    >>
    >> $_ = `ps aux|grep "test.pl $test" 2>&1`;
    >> my ($pid) = /(\d{3,})/;
    >> chomp($pid);
    >> open(TMP, ">/tmp/buj$pid") || die "Can't create: $!\n";
    >> close(TMP);
    >>
    >> my $i=0;
    >> while ($i<$duration) {
    >> sleep(1);
    >> $i++;}
    >>
    >> unlink "/tmp/buj$pid"; # erase the /tmp/bujPID file

    >
    >
    >
    > Can anyone else offer any advice or solutions to this problem? Or
    > does anyone know a better way to find the pid of a running perl
    > program?
    >
    > Thanks,
    > Dave
    >


    $$ gives you the pid of your currently running CGI program. To get the
    pid for the .pl file that you launch, you can use the open() command to
    launch it. When you use open to open a pipe, open() returns the pid of
    the process created. Read "perldoc -f open"

    For example, see this:

    --------------async.cgi----------------
    #!/usr/bin/perl

    use strict;
    use warnings;

    $| = 1;
    my $script = 'async.pl &';

    print "Content-Type: text/plain\n\n";
    my $pid = open(my $fh, '-|', $script) or die("Open failure for $script:
    $!");

    my $date = localtime;
    print qq{<info pid="$pid" date="$date" cgi_pid="$$">\n};

    close $fh;
    --------------end----------------------

    ----------------async.pl----------------
    #!/usr/bin/perl
    use strict;
    use warnings;

    print "Async.pl($0) running\n";

    sleep 10;
    ----------------end----------------------
     
    Mumia W., Jun 6, 2007
    #7
  8. Joe Smith Guest

    wrote:

    >>> if (system("./backup.pl \"$_\" 30 \&") == 0) {


    > Since the system call
    > has an appended & at the end to indicate to run as a seperate process,
    > the browser shouldn't be waiting on perl to finish that additional
    > script call, right?


    Using "&" alone is insufficient, since STDIN, STDOUT, and STDERR are
    still being held open by the backgrounded task.

    $log_file = '/dev/null'
    if (system qq(./backup.pl "$_" 30 </dev/null >>$log_file 2>&1 &) == 0 {


    -Joe
     
    Joe Smith, Jun 7, 2007
    #8
  9. Guest

    On Jun 7, 1:41 am, Joe Smith <> wrote:
    > wrote:
    > >>> if (system("./backup.pl \"$_\" 30 \&") == 0) {

    > > Since the system call
    > > has an appended & at the end to indicate to run as a seperate process,
    > > the browser shouldn't be waiting on perl to finish that additional
    > > script call, right?

    >
    > Using "&" alone is insufficient, since STDIN, STDOUT, and STDERR are
    > still being held open by the backgrounded task.
    >
    > $log_file = '/dev/null'
    > if (system qq(./backup.pl "$_" 30 </dev/null >>$log_file 2>&1 &) == 0 {
    >
    > -Joe



    Thanks for the responses Mumia and Joe. Joe your solution worked
    great. Would there be a problem embedding the $log_file value
    directly into the qq statement? I tried it and it worked just fine.
    I just wanted to make sure there wasn't something I was overlooking as
    to why I should use a variable instead of the direct value.

    Thanks,
    Dave
     
    , Jun 8, 2007
    #9
  10. Guest

    On Jun 8, 4:15 pm, wrote:
    > On Jun 7, 1:41 am, Joe Smith <> wrote:
    >
    > > wrote:
    > > >>> if (system("./backup.pl \"$_\" 30 \&") == 0) {
    > > > Since the system call
    > > > has an appended & at the end to indicate to run as a seperate process,
    > > > the browser shouldn't be waiting on perl to finish that additional
    > > > script call, right?

    >
    > > Using "&" alone is insufficient, since STDIN, STDOUT, and STDERR are
    > > still being held open by the backgrounded task.

    >
    > > $log_file = '/dev/null'
    > > if (system qq(./backup.pl "$_" 30 </dev/null >>$log_file 2>&1 &) == 0 {

    >
    > > -Joe

    >
    > Thanks for the responses Mumia and Joe. Joe your solution worked
    > great. Would there be a problem embedding the $log_file value
    > directly into the qq statement? I tried it and it worked just fine.
    > I just wanted to make sure there wasn't something I was overlooking as
    > to why I should use a variable instead of the direct value.
    >
    > Thanks,
    > Dave




    Gang,

    I have put together a solution based on the previous posts in
    this threat (posted below). Basically this script has two options
    that can be run, one to start an external pl script and the other
    regularly checks if its still running. The script seems to be running
    great from a browser point of view. However, when I run a "ps aux" at
    the prompt, I see a "[backup.cgi] <defunct>" for everytime the it was
    run (for the initialization and everytime it was checked to see if it
    was still running). I have googled for possible explainations and
    tried exiting right after the function has completed and tried closing
    STDOUT (commented out below), but without success. If anyone can help
    me out with this, I would greatly appreciate the help.

    Thanks,
    Dave



    backup.cgi
    ------------------
    #!/usr/bin/perl -w

    use strict;
    use CGI qw:)all);
    use CGI::Carp qw(fatalsToBrowser);

    if (defined param('trackStatus')) {
    $_ = param('trackStatus'); # this value contains the pid
    $_ = `ps h -p $_ 2>&1`;
    print "Content-type: text/html\n\n";
    if ($_ ne "") { print "processing"; } else { print "completed"; }
    #close (STDOUT);
    #exit 0;

    } elsif (defined param('runnow')) {
    $_ = param('runnow');
    if (system("./backup.pl \"$_\" 30 </dev/null >>/dev/null 2>&1 &")
    == 0) {
    $_ = `ps ax|grep "backup.pl $_"`;
    my ($pid) = /(\d{3,})/;
    my @info = stat("/tmp/buj$pid");
    print "Content-type: text/xml\n\n\n";
    print "<info pid=\"". $pid ."\" date=\"". scalar(localtime) ."\" />";
    }
    #close (STDOUT);
    #exit 0;
    }
    exit 0;
     
    , Jun 9, 2007
    #10
    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. bill sandner
    Replies:
    1
    Views:
    903
    iksrazal
    Jul 1, 2004
  2. system call and library call

    , Aug 22, 2005, in forum: C Programming
    Replies:
    7
    Views:
    3,055
    Jonathan Bartlett
    Aug 23, 2005
  3. leoman730

    System call and library call

    leoman730, May 3, 2007, in forum: C++
    Replies:
    6
    Views:
    934
    Jim Langston
    May 6, 2007
  4. David Pratt
    Replies:
    0
    Views:
    276
    David Pratt
    Apr 7, 2008
  5. David Pratt
    Replies:
    0
    Views:
    418
    David Pratt
    Apr 7, 2008
Loading...

Share This Page