Question

How to Prevent a User from Using CTRL+C or CTRL+Z to Stop a Script?

In some cases when writing a script you want to prevent the user from exiting the script with CTRL+C or CTRL+Z.

Here is how you could do that!


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hello,

Let’s take the following script as an example:

#!/bin/bash

function greeting(){
    echo "Enter your name"
    read name
    echo "Hi there ${name}"
}

# Call the function
greeting

With the above, we are expecting the user to enter their name and then greet them. However, the user could just press CTRL+C or CTRL+Z and exit the script.

To prevent that, we could use the trap command and trap specific term signals. For example, CTRL+Z sends the SIGTSTP signal, and CTRL+C sends SIGINT.

So if we wanted to prevent the user from exiting the script with CTRL+Z or CTRL+C, we could use the trap command and trap those signals:

#!/bin/bash

function greeting(){
    # Start the trap
    trap ' ' SIGTSTP SIGINT SIGTERM
    echo "Enter your name"
    read name
    echo "Hi there ${name}"
    # End the trap
    trap - SIGTSTP SIGINT SIGTERM
}

# Call the function
greeting

In case that you want to get a list of all term signals, you could use the following command:

trap -l

Output:

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

I personally prefer to use the numeric representation as follows:

...
    trap ' ' 2 15 20
    echo "Enter your name"
    read name
    echo "Hi there ${name}"
    # End the trap
    trap - 2 15 20
...

I hope that this helps! Regards, Bobby

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.