0

I'm sure this is really simple, but it's biting me in the face anyway, and I'm a little frustrated and stumped.

So, I have a script which I've managed to boil down to:

#!/bin/sh
sleep 50 | echo

If I run that at the command line, and hit Ctrl-C it stops, like I would expect.

If I send it sigint, using kill, it does nothing. I thought that was strange, since I thought those should have been the same.

Then, if I send it sigterm, then it also dies, but if I look in ps, the sleep is still running.

What am I missing, here?

This is obviously not the real script, which runs python, and it's more of a problem when it keeps running after start-stop-daemon tries to kill the daemon.

Help me people. I'm dumb.

1
  • Bash's mechanism probably somehow prevents SIGINT signals not sent by the keyboard to be handled.
    – konsolebox
    Aug 7, 2014 at 18:55

2 Answers 2

1

The reason this happens is that the Ctrl-C is delivered to the sleep process, whereas the sigint you are sending is delivered only to the script itself. See Child process receives parent's SIGINT for details on this.

You can verify this yourself by using strace -p when hitting ctrl-c or sending sigint; strace will tell you what signals are delivered.

EDIT: I don't think you are dumb. Processes and how they work are seemingly simple, but the details are often complicated, and even experts get confused by this sort of thing.

1
  • More specifically, Ctrl-C sends SIGINT to the current process group, which means the signal is propagated to all processes (the shell and both processes on either end of the pipe) in the group.
    – chepner
    Aug 7, 2014 at 19:07
0

I did the same thing I written script named as test.sh with below containt.

#!/bin/sh
sleep 50 | echo

After executing , I did Ctrl-C -> its working fine means closing it. Again executed and in another terminal i checked the PID by ps -ef|grep test.sh after finding the pid , i did kill <pid> and it killed the process , to verify again i executed ps -ef|grep test.sh and didnt get any pid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.