ksh child process not ignoring SIGTERM

My ksh version is ksh93-

  =>rpm -qa | grep ksh 
ksh-20100621-3.fc13.i686

I have a simple script which is as below -

#cat test_sigterm.sh -

  #!/bin/ksh
trap 'echo "removing"' QUIT 
while read line
do
sleep 20
done

I am Executing the script From Terminal 1 - 1. The ksh is started from /bin/ksh as below :

# exec /bin/ksh 
  1. The script is executed from this ksh-
# ./test_sigterm.sh&
[1] 12136

and Sending a "SIGTERM" From Terminal 2 -

# ps -elf | grep ksh 
4 S root     12136 30437  0  84   4 -  1345 poll_s 13:09 pts/0    00:00:00 /bin/ksh ./test_sigterm.sh
0 S root     18952 18643  0  80   0 -  1076 pipe_w 13:12 pts/5    00:00:00 grep ksh
4 S root     30437 30329  0  80   0 -  1368 poll_s 10:04 pts/0    00:00:00 /bin/ksh

# kill -15 12136

I can see that my test_sigterm.sh is getting killed on receiving the "SIGTERM" in either case, when run in background (&) and foreground. But the ksh man pages say - Signals. The INT and QUIT signals for an invoked command are ignored if the command is followed by & and the monitor option is not active. Otherwise, signals have the values inherited by the shell from its parent (but see also the trap built-in command below).
Is it a know or default behaviour of ksh to NOT IGNORE SIGTERM? or is an issue with ksh child SIGTERM signal handling?

I find nothing unusual in the behavior you describe; only an interactive shell should ignore SIGTERM. As kill's default signal, SIGTERM wouldn't be very useful if it were widely ignored.

Regards,
Alister

1 Like

The SIGTERM signal isnt being caught as it isnt SIGINT or SIGQUIT as the manpage says. If monitor option is off "set +o monitor" then sending a "kill -INT <pid>" or "kill -QUIT <pid>" wont terminate your job but not if you send a "kill -TERM <pid>" to it...

1 Like

As alister already said, the default signal handling behavior for SIGTERM is to terminate the process. Since you haven't ignored SIGTERM in your current shell and you don't ignore SIGTERM in test_sigterm.sh, test_sigterm.sh should terminate the process and that is exactly what you're describing. If you need test_sigterm.sh to ignore SIGTERM signals, change test_sigterm.sh from:

  #!/bin/ksh
trap 'echo "removing"' QUIT 
while read line
do
sleep 20
done

to:

#!/bin/ksh
trap '' TERM
trap 'echo removing' QUIT 
while read line
do
        sleep 20
done

Note that in addition to adding a trap to ignore SIGTERM, I:

  1. moved the #!/bin/ksh to the start of the line (with it where it was, the default shell for your system will be used to run this script instead of the Korn shell),
  2. removed the double quotes around removing (there is no need to quote a single word operand to the echo utility), and
  3. indented the body of the while loop (to make it easier to see the structure of your shell script).
1 Like