Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:36:20+00:00 2026-05-29T11:36:20+00:00

I have a process and want to start it again when it is killed.

  • 0

I have a process and want to start it again when it is killed. To achieve this, I start child “guardian” process, that uses prctl(PR_SET_PDEATHSIG, SIGHUP); to catch killing of its parent and starts it again.

Here is code of guardian (logging omitted):

void restart (int signal) {
    if (getppid() == 1) {
        if (fork() == 0) {
            execl("./process", 0);
        }
        exit(1);
    }
}

int main() {
    prctl(PR_SET_PDEATHSIG, SIGHUP, NULL, NULL, NULL);

    struct sigaction new_action, old_action;
    new_action.sa_handler = restart;
    sigemptyset (&new_action.sa_mask);
    new_action.sa_flags = 0;

    sigaction (SIGHUP, NULL, &old_action);

    if (old_action.sa_handler != SIG_IGN) {
        sigaction (SIGHUP, &new_action, NULL);
    } 

    while (getppid() != 1) {
        sleep(86400000);
    }
    return 0;
}

And parent:

int main() {
    if (fork() == 0) {
        execl("./guardian", 0);
    } 
    while (1) {
        cout << "I am process\n";
        sleep(1);
    }
    return 0;
}

The problem I have is that it works just one time. Here is ps output when process was started first time:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
1012     13058  0.0  0.3  20244  1932 pts/1    Ss   08:22   0:00 -sh
1012     22084  0.0  0.1  11484  1004 pts/1    S+   11:20   0:00  \_ ./process
1012     22085  0.0  0.1  11484  1000 pts/1    S+   11:20   0:00      \_ [guardian]
1012     12510  0.0  0.3  20784  1712 pts/0    Ss   08:14   0:00 -sh
1012     22088  0.0  0.1  17412  1012 pts/0    R+   11:20   0:00  \_ ps fu

which looks good. Next I kill process with kill -9 22084. And again ps output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
1012     13058  0.0  0.3  20244  1932 pts/1    Ss+  08:22   0:00 -sh
1012     12510  0.0  0.3  20784  1712 pts/0    Ss   08:14   0:00 -sh
1012     22091  0.0  0.1  17412  1012 pts/0    R+   11:21   0:00  \_ ps fu
1012     22089  0.0  0.1  11484   996 pts/1    S    11:20   0:00 [process]
1012     22090  0.0  0.1  11484   996 pts/1    S    11:20   0:00  \_ [guardian]

and when I kill process again kill -9 22089 guardian does not seem to get SIGHUP callback (I checked from logs, they are omitted here).

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
1012     13058  0.0  0.3  20244  1932 pts/1    Ss+  08:22   0:00 -sh
1012     12510  0.0  0.3  20784  1712 pts/0    Rs   08:14   0:00 -sh
1012     22339  0.0  0.1  17412  1008 pts/0    R+   11:27   0:00  \_ ps fu
1012     22090  0.0  0.1  11484   996 pts/1    S    11:20   0:00 [guardian]

My question is – why guardian does not get SIGHUP?

I suspect it might have something to to with background process group – when process is restarted it is in background group (compare S+ and S in ps stat).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:36:20+00:00Added an answer on May 29, 2026 at 11:36 am

    It looks like SIGHUP is blocked while you are in the signal handler handling SIGHUP. fork() and exec() inherit the signal mask, hence your second guardian never receives it again.

    Unblock SIGHUP while in the signal handler after fork() before exec() parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a process x that I want to check for leaks with valgrind
I have a c++ process, I want that process should always remain on foreground,
I have the following XML and I want to process it so that I
I have some data generated in MATLAB that I want to process using Perl.
I have a set of .csv files that I want to process. It would
Say I have a data file that I want to process; I want to
I have a DIV tag which when clicked I want to start a process,
I have a process I want to run in the background of a page.
I have a process which gives me continuously output in Screen. I want to
I have a process running under Windows Server 2003 SP2. When I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.