Shell – Disable ctrl c and ctrl z while a script is running

shell-scriptsignalsterminal

I have a script that takes about 45 seconds to run. How can I disable Ctrlc and Ctrlz while a script is running?

Best Answer

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)