5

I have a script that takes about 45 seconds to run. How can I disable Ctrl-c and Ctrl-z while a script is running?

7

Although you could disable Ctrl-c and Ctrl-z by disabling those terminal settings or setting the terminal to raw more or other solutions, you are usually much better off leaving them enabled and reacting to the resulting signals instead. You can install handlers for the signals and let the handlers do nothing.

The way to install handlers depends on the programming language you are using. You haven't said what you are using but here are a few examples:

Shell:

#!/bin/sh

trap '' INT TSTP

Python:

#!/usr/bin/python

import signal

signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTSTP, signal.SIG_IGN)
  • I am using #!/bin/bash – tester787 Aug 3 '15 at 19:04
  • I was hoping to put some commands inside the script so it can not be interrupted while it is running. – tester787 Aug 3 '15 at 19:05
  • @tester787 yes, that is exactly what I suggest that you do! – Celada Aug 3 '15 at 20:04
  • Edited to remove PEBKAC... ;-) – Fabby Aug 3 '15 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.