C 實現 HUP 訊號重啟進程

來源:互聯網
上載者:User
/***************************************************************************** *  Copyright          :  All Rights Reserved. * *  Date               :  2013-01-11 17:02:10 *  Author/Corporation :  Dengzhaoqun *  Email              :  dengzhaoqun@163.com *****************************************************************************/#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] 盡量使用絕對路徑, 因為程式執行過程中可能會使用 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_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);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.