The method of realizing HUP signal restart process with C language _c language

Source: Internet
Author: User
Tags openlog syslog
The code looks like this:
Copy Code code as follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <syslog.h>
#include <stdarg.h>
Char **bak_argv;
int flag = 0;
void restart (void);
void Sighup_handler (int sig);
Argv[0] Use absolute paths as much as possible, because ChDir () may be used during program execution.
int main (int argc, char *argv[])
{
Openlog ("Sig_hup", Log_pid, 0);
Syslog (Log_err, "%s", argv[0]);
Closelog ();

BAK_ARGV = argv;
Signal (Sighup, Sighup_handler);

while (1)
{
if (flag)
Restart ();
Sleep (10);
}
return 0;
}
void Sighup_handler (int sig)
{
flag = 1;
}
void restart (void)
{
Switch (fork ())
{
Case-1:
Openlog ("Sig_hup", Log_pid, 0);
Syslog (Log_err, "Fork failed:%s", Strerror (errno));
Closelog ();

Exit (Exit_failure);
Break
Case 0:
Break
Default
Exit (exit_success);
}
EXECV (Bak_argv[0], bak_argv);

Openlog ("Sig_hup", Log_pid, 0);
Syslog (Log_err, "Execv" (%s, ...) Failed:%s\n ", Bak_argv[0], strerror (errno));
Closelog ();

Exit (Exit_failure);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.