Does bash -c work like nohup?

while working with the old initscript in redhat, I came across a line like

daemon --user $USER --pidfile $PIDFILE "cmd >>/var/log/cmd.log 2>&1 &"

It seemed suspicious that he was not using nohup. However, the program works correctly and continues to work even if the control terminal exits. Examining further, I found that the command, for example, bash -c 'sleep 1000&'seems to remain valid even if the terminal from which it is being called is completed (which should not have been possible without nohup). I tested this behavior with the latest ubuntu.

So my question is: is this behavior good? that is, can I use it in my init script instead of using nohup? or is it a bug in bash?

+5
source share
1 answer

: SIGHUP , ? bash shopt:

huponexit , bash SIGHUP , .

Ubuntu 12.04 huponexit . , :

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

void
hupped(int i)
{
    fprintf(stderr, "received SIGHUP 0x%x\n", i);
    exit(1);
}

int main(int argc,char * argv[])
{
    fprintf(stderr, "registering SIGHUP handler for PID %d\n", getpid());
    signal(SIGHUP, hupped);
    sleep(3600*5);
    return 0;
}

stdin stdout tty, , . , init, pty .

, SIGHUP "" bash. , , , , .

+5

All Articles