Linux守护进程断开,c-守护进程未在Linux中停止

我使用了以下指南(http://peterlombardo.wikidot.com/linux-daemon-in-c),它运作良好且美观,接受它不会杀死您.

Main.cpp

//Global Directives

//#define DEBUG 1

#define DAE_NAME "dae"

#define DAE_PID "/var/run/dae.pid"

//Includes

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

//Namespace (System)

using namespace std;

//Classes

#include "class.h"

//Structures

#include "struct/struct.h"

//Functions

#include "function/signal_handler.h"

void usage(int argc, char *argv[]) {

if (argc >=1) {

printf("Usage: %s -h -nn", argv[0]);

printf(" Options:n");

printf(" -ntDon't fork off as a daemon.n");

printf(" -htShow this help screen.n");

printf("n");

}

}

int main(int argc, char *argv[]) {

#if defined(DEBUG)

int daemonize = 0;

#else

int daemonize = 1;

#endif

// Setup signal handling before we start

signal(SIGHUP, signal_handler);

signal(SIGTERM, signal_handler);

signal(SIGINT, signal_handler);

signal(SIGQUIT, signal_handler);

//Print Usage Information

int c;

while((c = getopt(argc, argv, "nh|help")) != -1) {

switch(c){

case 'h':

usage(argc, argv);

exit(0);

break;

case 'n':

daemonize = 0;

break;

default:

usage(argc, argv);

exit(0);

break;

}

}

// Setup syslog logging

#if defined(DEBUG)

setlogmask(LOG_UPTO(LOG_DEBUG));

openlog(DAE_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);

#else

setlogmask(LOG_UPTO(LOG_INFO));

openlog(DAE_NAME, LOG_CONS, LOG_USER);

#endif

//------------

//Daemon Setup

//------------

pid_t pid, sid;

if (daemonize) {

syslog(LOG_INFO, "Starting %s", DAE_NAME);

/* Fork off the parent process */

pid = fork();

if (pid < 0) {

exit(EXIT_FAILURE);

}

/* If we got a good PID, then

we can exit the parent process. */

if (pid > 0) {

exit(EXIT_SUCCESS);

}

/* Change the file mode mask */

umask(0);

/* Create a new SID for the child process */

sid = setsid();

if (sid < 0) {

/* Log the failure */

exit(EXIT_FAILURE);

}

/* Change the current working directory */

if ((chdir("/")) < 0) {

/* Log the failure */

exit(EXIT_FAILURE);

}

/* Close out the standard file descriptors */

close(STDIN_FILENO);

close(STDOUT_FILENO);

close(STDERR_FILENO);

}

int count = 1;

//-------

//Process

//-------

while (1){

process();

syslog (LOG_INFO, "%s Processed(%s, %d)", DAE_NAME, getlogin(), count);

count++;

}

syslog (LOG_INFO, "Exiting Daemon %s", DAE_NAME);

closelog (); //Close the Connection

exit(EXIT_SUCCESS);

}

singal_handler.h

#ifndef SIGNAL_HANDLER_H_

#define SIGNAL_HANDLER_H_

void signal_handler(int sig) {

switch(sig) {

case SIGHUP:

syslog(LOG_WARNING, "Received SIGHUP signal.", DAE_NAME);

break;

case SIGTERM:

syslog(LOG_WARNING, "Received SIGTERM signal.", DAE_NAME);

break;

default:

syslog(LOG_WARNING, "Unhandled signal ", DAE_NAME);

break;

}

}

#endif

命令行

所以我在命令行中运行

./dae

然后我跑

kill procid

日志

INFROMATIONAL, Starting dae

INFORMATIONAL, dae Processed((null), 1)

INFORMATIONAL, dae Processed((null), 2)

INFORMATIONAL, dae Processed((null), 3)

WARNING, Received SIGTERM signal.

INFORMATIONAL, dae Processed((null), 4)

INFORMATIONAL, dae Processed((null), 5)

该死的事情没有停止吗?任何想法谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值