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.
When you
trapa 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' SIGINTprevents ctrl+C from having its default behavior, and thetrap 'send_signal 3' SIGSTPprevents ctrl+Z from having its default behavior.