0

I am converting an interactive command line tool to a web application, with the tool as the backend. I take in user commands (using AJAX) and call a perl CGI script that extracts the command. I then use expect to send the command to the process, collect the output and pass that to the resultant html page.

The first command that the user inputs executes fine. The next commands aren't executed.

I am using FreezeThaw to freeze the expect object after the first request and then to thaw it for the following requests. It freezes fine, but doesn't thaw.

Here's my code:

use strict;
use warnings;
use CGI;
use Expect;
use FreezeThaw qw(freeze thaw);

if ( -e "logFile" ) {
    ##Log file exists, just run the command after retrieving the object
    ##Retrieve object here
    my ($expectObject) = thaw( $params{'object'} );

    if ( $command eq 'exit' ) {

        #exit
    }
}
else {
    print "log NOT exists!!";
    ##Log file doesn't exist, spawn a new process and loop
    my $expectObject = Expect->spawn("command") || die "\nCannot spawn: $!\n";
    $expectObject->expect( 15, "prompt>" );
    $expectObject->send("$command\r");
    $expectObject->expect( 15, "stile>" );
    $output = $expectObject->before();
    print "<br>$output<br>";

    ##Persist object here in file
    my $serialized = freeze($expectObject);
    ##Write serialized object to file
    die "Serialization Error (write):\n$!" if ( !addParameter( "$workingDir/$folderName", "object", $serialized ) );
}

Any ideas why is it failing..?

matthias krull
  • 4,389
  • 3
  • 34
  • 54
krish7919
  • 892
  • 2
  • 13
  • 30
  • And can anyone pls tell me if my CGI script ends, does it also kill the spawned proces..? If yes, how can I make the spawned process persist across requests..? – krish7919 Apr 02 '12 at 10:44

1 Answers1

1

If a Perl CGI program ends, it will destroy all spawned process if they does not daemonize itself.

Use mod_perl or other persistent mechanism to keep open a 'shell/command' or execute all commands one by one.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
user1126070
  • 5,059
  • 1
  • 16
  • 15
  • Well, at least you hope it cleans up all spawned processes :) – brian d foy Apr 02 '12 at 18:26
  • I did read a little about mod_perl. Can you please give me an example or guide me to an excellent tutorial site..? The apache site mentions lots of features, but I cannot find examples. Thank! And @user1126070, can you give me ideas about any other persistent mechanism please? – krish7919 Apr 03 '12 at 19:45
  • Why do you need persistance at all? – user1126070 Apr 05 '12 at 11:08