Expect script - run code on exit

979 views Asked by At

Is it possible to run code when an expect script it terminated?

Given the following example;

#!/usr/bin/expect

while { true } {
  puts "I am alive"
  sleep 5
}

puts "I am dead"

This will continuously print "I am alive". When I press CTRL+C to kill the script, how can I call a function (or similar) to print "I am dead" on the way out?

1

There are 1 answers

0
resmon6 On BEST ANSWER

This link explains how to handle SIGINT in Expect. This is what you want to do in your code:

#!/usr/bin/expect

proc sigint_handler {} {
  puts "I am dead"
  exit
}
trap sigint_handler SIGINT

while { true } {
  puts "I am alive"
  sleep 5
}