Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

[Edit: This looks similar to some other questions asking how to kill all spawned processes – the answers all seem to be to use pkill. So the core of my question may be: Is there a way to propagate Ctrl-C/Z to all processes spawned by a script?]

When calling a SoX rec with the timeout command from coreutils (discussed here), there doesn't seem to be any way to kill it with a keystroke once it's been invoked from within a Bash script.

Examples:

timeout 10 rec test.wav

...can be killed with Ctrl+C or Ctrl+Z from bash, but not when it's been called from inside a script.

timeout 10 ping nowhere

...can be killed with Ctrl+C or Ctrl+Z from bash, and with Ctrl+Z when it's run from inside a script.

I can find the process ID and kill it that way, but why can't I use a standard break keystroke? And is there any way to structure my script so that I can?

share|improve this question
2  
Ctrl+Z does not kill processes, only pauses them. They will keep running if you give the bg of fg commands. Anyway, is there a difference between your 1st and 3d examples? –  terdon Dec 5 '12 at 20:24
    
I don't have timeout on my system but killing sleep works whether it's typed directly on the command line, sourced, executed, or passed through the interpreter explicitly –  Kevin Dec 5 '12 at 21:18
    
@terdon Thanks, I've clarified the examples. –  meetar Dec 8 '12 at 17:28
add comment

2 Answers

up vote 3 down vote accepted

Signal keys such as Ctrl+C send a signal to all processes in the foreground process group.

In the typical case, a process group is a pipeline. For example, in head <somefile | sort, the process running head and the process running sort are in the same process group, as is the shell, so they all receive the signal. When you run a job in the background (somecommand &), that job is in its own process group, so pressing Ctrl+C doesn't affect it.

The timeout program places itself in its own process group. From the source code:

/* Ensure we're in our own group so all subprocesses can be killed.
   Note we don't just put the child in a separate group as
   then we would need to worry about foreground and background groups
   and propagating signals between them.  */
setpgid (0, 0);

When a timeout occurs, timeout goes through the simple expedient of killing the process group of which it is a member. Since it has put itself in a separate process group, its parent process will not be in the group. Using a process group here ensures that if the child application forks into several processes, all its processes will receive the signal.

When you run timeout directly on the command line and press Ctrl+C, the resulting SIGINT is received both by timeout and by the child process, but not by interactive shell which is timeout's parent process. When timeout is called from a script, only the shell running the script receives the signal: timeout doesn't get it since it's in a different process group.

You can set a signal handler in a shell script with the trap builtin. Unfortunately, it's not that simple. Consider this:

#!/bin/sh
trap 'echo Interrupted at $(date)' INT
date
timeout 5 sleep 10
date

If you press Ctrl+C after 2 seconds, this still waits the full 5 seconds, then print the “Interrupted” message. That's because the shell refrains from running the trap code while a foreground job is active.

To remedy this, run the job in the background. In the signal handler, call kill to relay the signal to the timeout process group.

#!/bin/sh
trap 'kill -INT -$pid' INT
timeout 5 sleep 10 &
pid=$!
wait $pid
share|improve this answer
    
Very sneaky - worked beautifully! I am much smarter now, merci! –  meetar Dec 6 '12 at 17:12
add comment

I think you do not understand what is going on here.

Basically Ctrl+C sends a SIGINT signal, while Ctrl+Z send a SIGTSTP signal.

SIGTSTP just stops the process - a SIGCONT will continue it.

This works on the forground-process that was forked on the command-line.

If your process is a background-process you will have to send that signal to the processes in a different way. kill will do that. In theory a "-"-operator on that kill should also signal child processes but this seldom works as expected.

For further reading: Unix-Signals

share|improve this answer
    
This is true, at least up to “seldom works as expected” (which depends on your expectations — you should read up about process groups), but completely irrelevant. The question does not betray any confusion about SIGINT and SIGTSTP. –  Gilles Dec 6 '12 at 1:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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