Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to have a wrapper program that runs a given command and sets a signal handler so that it gets run when the command receives a specified signal.

The question is this:

  1. Is there an utility program to do this?
  2. If not, is it possible to do this by using bash's commands trap and exec?
  3. If not, how can I do this? (e.g. by writing a program myself in C which does a few system calls)

EDIT: The target platform is GNU/Linux.

EDIT 2: Following Ignacio's answer, I managed to write a preload SO which looks like this.

share|improve this question
up vote 4 down vote accepted

Can't be done. From the exec(3p) man page:

Signals set to be caught by the calling process image shall be set to the default action in the new process image.

You would have to write a preload SO which would hook up the signal handlers before the program started.

share|improve this answer
Is that true? The GNU implementation of nohup(1) calls signal(SIGHUP, SIG_IGN) followed by exec(3), and it can effectively make the run command immune to SIGHUP. – Pteromys Nov 7 '11 at 4:05
SIG_IGN is not "caught". "Except for SIGCHLD, signals set to be ignored (SIG_IGN) by the calling process image shall be set to be ignored by the new process image." – Ignacio Vazquez-Abrams Nov 7 '11 at 4:07
Thanks. (I should have read the POSIX document more carefully.) As for a preload shared object to do this, do I have to write it myself? I think such an SO is versatile, and suspect there exist a well-known library to do this. – Pteromys Nov 7 '11 at 4:15
The thing of it is that you have to write the signal handlers yourself, so it would end up being a mostly custom job regardless. – Ignacio Vazquez-Abrams Nov 7 '11 at 4:17
@Pteromys And the reason a program can't set a signal handler for another program is that the code would execute in the program where it resides. If the process calls execve, all of its code and data is freed from memory, so it can't be used for a signal handler any more. – Gilles Nov 7 '11 at 23:43

You could only have it ignore signals (SIG_IGN); you wouldn't be able to give a signal handler either from the shell (trap) or within a C program (signal(2)). When exec is called, any signal handler would be replaced.


trap "" 1 2
exec sleep 100000

Then call the program, either in the background or fore, and send either a SIGHUP or SIGINT to the program. It is ignored. Putting code in the trap causes signal to be reset on exec:

trap "echo signal caught" 1 2
exec sleep 10000

This won't print "signal caught" of you send the signals, but will in fact terminate the program.

share|improve this answer

Your Answer


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.