How do I launch a background process through a wra

2019-08-02 18:05发布

I want to launch a background process from bash in such a way that I can then send SIGINT signals to it (eg: via kill or htop). How do I do that?

By default, when a process is launched in the backround, SIGINT and SIGQUIT are ignored (see this question).

1条回答
地球回转人心会变
2楼-- · 2019-08-02 18:35

If the process is execed (=isn't just a subshell but is based on binary), you can run it through a wrapper that'll undo the SIGINT/SIGQUIT ignore:

reset_sigint.c:

#include <signal.h>
#include <unistd.h>
int main(int C, char**V)
{
    sigaction(SIGINT,&(struct sigaction){.sa_handler=SIG_DFL}, 0);
    execvp(V[1],V+1);
    return 127;
}

To prevent the SIGINT/SIGQUIT ignore more generically for any process run as a command in the shell, you can run it with set -m on

sh -c 'set -m; ( sleep 10 )& ps -p $! -o ignored'
#compare with: sh -c '( sleep 10 )& ps -p $! -o ignored'

but that'll run the command in a separate process group as a (possibly undesirable) sideffect.

查看更多
登录 后发表回答