Perl Expect with FreezeThaw

232 views Asked by At

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..?

1

There are 1 answers

3
user1126070 On BEST ANSWER

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.