Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child process should not die with parent process

The following scenario:

  • process_1.bin starts doSomeThing.sh

  • doSomeThing.sh should kill process_1.bin first but keep running itself and do some other things

I have tried fork, exec, screen, p_threads, daemon, and some without success. Every time the doSomeThing.sh starts and kills the process_1.bin, it kills itself because the parent process is killed.

doSomeThing.sh tells me: "leaving program after kill signal!"

Unfortunately, this does not work either:

        pid_t pid;
        pid = fork();
        if (pid < 0)
            exit(EXIT_FAILURE);

        if (pid > 0)            // Success: Let the parent terminate
            exit(EXIT_SUCCESS);

        if (setsid() < 0)
            exit(EXIT_FAILURE);

        pid = fork();
        if (pid < 0)
            exit(EXIT_FAILURE);

        if (pid > 0)
            exit(EXIT_SUCCESS);
    
    //     umask(0);  

    // chdir("/");

        switch(pid)
        {
        case -1: // Fork() has failed
            perror ("fork");
            break;
        case 0: // This is processed by the child
// deamon(0,0);            
          system("/root/doSomeThing.sh");
// system("screen -dmS doSomeThing /root/doSomething.sh")

//     char *name[] = {
//    "/bin/bash",
//    "-c",
//    "/root/doSomeThing.sh",
//    NULL
//    };
//     if(execvp(name[0], name) < 0)
//       perror("execvp");

            exit(0);
            break;
        default:    // This is processed by the parrent
            break;
        }

How can I direct the doSomeThing.sh to kill the process_1.bin but still stay alive itself?

like image 906
oemer_1907 Avatar asked Nov 20 '25 12:11

oemer_1907


1 Answers

Apart from ignoring SIGTERM as suggested by Jonathan Leffler, you could make the child process a daemon so that its life will become independant of the life of its parent. Linux has a command to start a process as a daemon daemonize or a library function for a program willing to detach from its parent and its controlling terminal daemon.

like image 88
Serge Ballesta Avatar answered Nov 23 '25 01:11

Serge Ballesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!