h.chris has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm new to perl and learning to develop my first Perl-CGI HTML page. I have a bash script that run few of the database functions within our organization, part of the script while performing different operations it does show a progress bar displaying (.)dots on the terminal

There is a requirement and now we want the script to be run from HTML page wherein our developers could use the webpage to run the script without actually connecting to the server. After investigating found Perl-CGI is a good option and Initially I had problem with perl based buffering while running the script through perl-CGI. However I fixed the issue following suggestions from other posts by using ($|=1;) to generate un buffered perl output.

After fixing the buffer issue, now I have 2 issues. please see below,

1) while I execute the perl script from terminal the output does display (.)dots, However they appear in wrong order....

2) If I execute from the webpage - output doesn't display (.)dots - however waits until it finishes the function + wrong order In order to help debugging the script - I made sample BASH and PERL to re-generate the same issue, I am attaching the BASH and Perl code here. Please see and advise if we can achieve OUTPUT displaying the progress bar and also in the correct order

Bash_Script: Name: shell_script.sh
#!/bin/bash ### rundots() { ( trap 'exit 0' SIGUSR1; while : ; do echo -ne '.' >&2; +sleep 0.2; done) & dotpid=$!; } ### stopdots() { kill -USR1 $dotpid; wait $dotpid; trap EXIT; } ### startdots() { echo -e "\nExecuting function >>>> $(tr [a-z] [A-Z] <<< "${FUNCNAME[ 1 + ]}") <<<<<" echo -ne "---->>Script executing, please wait" rundots; trap "stopdots" EXIT; return 0; sleep 0.3} ### main1() { startdots echo -e "display space usage" > /tmp/shell_script.log sleep 4 du -sh * >> /tmp/shell_script.log stopdots } main2() { startdots echo -e "list files," >> /tmp/shell_script.log sleep 4 ls -lsrt >> /tmp/shell_script.log stopdots echo -e "\n\n*********** END OF THE BASH SCRIPT ************" } ### call functions main1 main2
Perl-CGI: Name: perl_script.pl
#!/usr/bin/perl -w $|=1; use CGI qw(:standard); #use IO::Handle; use FileHandle; print "Content-type: text/html\n\n"; print '<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Page title</title> <link rel="stylesheet" href="hari.css" type="text/css" media=" +screen"> </head>'; open (my $cmds, "/var/www/cgi-bin/shell_script.sh |"); open (my $LOG, '>', '/tmp/perl.log'); select((select($LOG), $|=1)[0]); while (<$cmds>) { #print "<pre>"; print; print $LOG $_; #print "</pre>"; } close $cmds; close $LOG; print ' </body> </html>';
Thanks in advance.

Replies are listed 'Best First'.
Re: Perl CGI + Bash script output in wrong order
by perlfan (Parson) on Jun 09, 2014 at 14:45 UTC
    Dear God, please make sure you put some authentication and restricted IP in front of this.

    FWIW, you can just output HTTP headers via the bash script, you don't need to wrap that up into a Perl script. A CGI program can be *anything*.

    Also, please don't do it this way. You never want a direct URL to execute a script. Even if you have authentication locked down, you may accidentally call it when you really don't want to.

      Hi,

      Thanks for your suggestions, I did learn we can use BASH to output HTTP headers - however I want move away from bash, so thought this will be a good chance to start with.

      With regards to URL executing script - As I mentioned on my original post, this is just a sample script to re-generate the issue - Original script is NOT a direct URL based one !

      *Please note: the scripts attached are just for debugging purpose

      Regards,

Re: Perl CGI + Bash script output in wrong order
by neilwatson (Priest) on Jun 09, 2014 at 15:26 UTC
Re: Perl CGI + Bash script output in wrong order
by h.chris (Initiate) on Jun 10, 2014 at 14:24 UTC

    Hi Thanks everyone, I fixed the problem. It is do with "|" while executing the bash script

    Regards,

    Hari
      Hi Hari, I am in a similar situation and would request your help. can you please elaborate on how did you fix the issue? Thanks RS