voidsig_term(int signo) { if(signo == SIGTERM) /* catched signal sent by kill(1) command */ { wsio_logit("", "wsiod stopped/n"); exit(0); } }
/* main program of daemon */ intmain(void) { if(init_daemon() == -1){ printf("can't fork self/n"); exit(0); } wsio_logit("", "wsiod started/n"); signal(SIGTERM, sig_term); /* arrange to catch the signal */
while (1) { // Do what you want here … … } exit(0); }
daemon 程序管理脚本
daemon 程序可以使用 service 工具进行管理,包括启动、停止、查看状态等,但前题是需要编写一个如下的简单 SHELL 脚本,比如 /etc/init.d/wsiod :
#!/bin/sh # # wsiod This shell script takes care of starting and stopping wsiod. # # chkconfig: 35 65 35 # description: wsiod is web servce I/O server, which is used to access files on remote hosts.
# Source function library. . /etc/rc.d/init.d/functions