日期:2014-05-16 浏览次数:20736 次
#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;
void sighup_restart(int sig);
// argv[0] 尽量使用绝对路径, 因为程序执行过程中可能会使用 chdir() .
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_restart);
while(1)
sleep(10);
return 0;
}
void sighup_restart(int sig)
{
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);
}