I used the following guide (http://peterlombardo.wikidot.com/linux-daemon-in-c), and it works fine and beautifully, admits that it does not kill.
main.cpp
#define DAE_NAME "dae"
#define DAE_PID "/var/run/dae.pid"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
using namespace std;
#include "class.h"
#include "struct/struct.h"
#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
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
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;
}
}
#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
pid_t pid, sid;
if (daemonize) {
syslog(LOG_INFO, "Starting %s", DAE_NAME);
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
int count = 1;
while (1){
process();
syslog (LOG_INFO, "%s Processed(%s, %d)", DAE_NAME, getlogin(), count);
count++;
}
syslog (LOG_INFO, "Exiting Daemon %s", DAE_NAME);
closelog ();
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
Command line
So, on the command line, I ran this
./De
And then I ran
kill procid
Magazines
INFROMATICS, the beginning of dae
INFORMATION, dae Processed ((null), 1)
INFORMATION, dae Processed ((null), 2)
INFORMATION, dae Processed ((null), 3)
WARNING, received SIGTERM signal.
INFORMATION, dae Processed ((null), 4)
INFORMATION, dae Processed ((null), 5)
Damn thing doesn't stop? Any ideas thanks