Trap ctrl-z key to perform cleanup

1

All,
I have a shell script where I want to call a function to do some cleanup if user presses the ctrl-z key(SIGTSTP signal). I read about the trap command and found an example where I can trap the ctrl-c key. Is there a way to intercept the SIGTSTP signal?

smokinguns

Posted 2011-10-24T05:18:26.493

Reputation: 1 188

Just follow man page. – Prince John Wesley – 2011-10-24T06:44:31.473

Answers

2

#!/bin/bash
# ctrl + z handler 
function suspendHandle() {
 echo "$@"
}
# trap the SIGTSTP signal
# suspendHandle is a handler function with the parameters "trapping ctrl + z"
trap "suspendHandle trapping ctrl + z" 20 
# send SIGTSTP signal to current shell
kill -s 20 $$ 

Prince John Wesley

Posted 2011-10-24T05:18:26.493

Reputation: 2 139