/*************************************** * *********************************** Copyright: all rights reserved. ** Date: 17:02:10 * Author/Corporation: dengzhaoqun * Email: *************************************** **************************************/# 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 path whenever possible, because Program Chdir () may be used during 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 );}