Baldrick
Baldrick

Reputation: 11002

Expect script - run code on exit

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?

Upvotes: 1

Views: 985

Answers (1)

resmon6
resmon6

Reputation: 847

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
}

Upvotes: 2

Related Questions