2

I'm trying to use expect to spawn a program in an automation script in perl.

And I'm trying to decide how to wait on expect for this program to finish, since - I can't rely on any string matching as the program is not consistent with how it exits - I would like to wait on expect until the user prompt is seen and the user gets control.

Is there any means of doing it without performing a regex on the user prompt ? Any flags or exit codes I can rely on which tells the user has control now.

Thanks

pynexj
  • 19,215
  • 5
  • 38
  • 56
seek
  • 41
  • 3
  • Can you clarify your first sentence? Are you using perl's expect, or the plain Tcl expect and the spawned program is implemented in perl? Do you actually require expect for this task? – glenn jackman Nov 08 '18 at 03:52
  • Yes I'm using perl's expect. And the spawned program/task is basically a git clone of a remote repository. Now I'm unable to precisely tell when the cloning process is done and if I can proceed with the rest of my automation script – seek Nov 08 '18 at 04:34
  • 1
    If there is no interaction why use `Expect`? How is the program "_inconsistent_" -- what are other ways for it to finish, other than just exit? – zdim Nov 08 '18 at 05:59

1 Answers1

2

It is not stated what else Expect is used for, or how else the program may indicate its exit.

Assuming that at one point interaction stops and we only wait for the program to exit, you can use expect(undef)

use warnings;
use strict;
use feature 'say';

use Expect;

my $cmd = 'ls -l ./ | head -5; sleep 3';

my $exp = Expect->spawn( $cmd );
say "Started process ", $exp->pid;   

$exp->raw_pty(1);
$exp->log_stdout(0);
# ...

$exp->expect(undef);
say "Program exited with status ", $exp->exitstatus;

say $exp->before;

If no output is expected after the program goes incommunicado remove before.

Another way is to set up a $SIG{CHLD} signal handler, where you check for the program's PID and set a flag that other code can then check. The PID is in a variable which need be declared before the handler and then set with pid method after the process is started, so that it is legal (under strict) to use in the handler and it is set for when the handler runs.

Then exitstatus isn't useful (-1) as the process is reaped in the handler.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • is there a way to check if the $cmd is successful ? – tourist Mar 24 '22 at 13:36
  • 1
    @sbk Not sure how much one can check, will have to review what the module offers (haven't used it in years). I'll let you know when I get to look it up ... (or perhaps I'll answer your recent question if I find enough interesting information). – zdim Mar 24 '22 at 19:16
  • I don't see any generic checks (except for `exitstatus`) in docs so you'd depend on what the programs (that Expect runs) do and provide -- their output, error/return codes, etc. For those, Expect does have ways to see them, and/or you can use OS/Perl's generic error-related tools/variables – zdim Mar 29 '22 at 16:42
  • Thanks for the information. HåkonHægland suggested an approach here: https://stackoverflow.com/a/71651729/5070050 , I will try that. – tourist Apr 04 '22 at 10:18