(Bash, terminal) why do killing script hotkeys don't work (e.g. ctrl-c, ctrl-z)

56 views Asked by At

when i try to kill the script by ctrl-z or ctrl-c they are interpreted like input and not commands. I'm using linux (fedora 37) on a virtual box machine

terminal:

[me@meVPC ~]$ ./time_logger
9698
^C
^C
^Z

the script: `

#!/bin/bash

log_file="time_log.txt"
signal_script="script_logger"

# Start the signal script
bash $signal_script &

# Function to send signals
send_signal() {
    signal=$1
    pkill -RTMIN+$signal -f $signal_script

}

# Signal handling
trap 'send_signal 2' SIGINT   # Signal for script stop
trap 'send_signal 3' SIGTSTP   # Signal for script pause
trap 'send_signal 4'  SIGCONT  #signal for script continue

send_signal 1               #sending signal that script started
echo $$                      #showing PID of this process

while true
    do
    current_time=$(date +"%Y-%m-%d %H:%M:%S")
    echo "$current_time" >> $log_file
    sleep 60
done`

i tried killing it from another bash terminal window, strangely it works half the times.

1

There are 1 answers

0
Charles Duffy On

When you trap a signal, that doesn't just add extra behavior before the signal is handled -- it outright replaces the default signal handler.

Thus, the trap 'send_signal 2' SIGINT prevents ctrl+C from having its default behavior, and the trap 'send_signal 3' SIGSTP prevents ctrl+Z from having its default behavior.