Search code examples
cunixdaemonsyslog

Redirect STDERR from service to syslog


I have to run a Perl script as a service (i.e daemon). I found the C program below and in general it works fine. However, in case the perl scripts dies with an exception I don't get any error message. How can I get the error from perl?

Terminal (i.e. STDERR) is not available, I assume the best idea would be to get the error messages in syslog.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>


int main( int argc, char *argv[] ) {    
    pid_t pid, sid;    

    if( argc < 1 ) {
        printf("At least one argument expected.\n");
        return EXIT_FAILURE;
    }

    char arg[1000];
    strcpy(arg, argv[1]);
    int i = 2;
    for(i = 2; i < argc; i++) {
        strcat(arg, " ");
        strcat(arg, argv[i]);       
    }

    /* 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);
    }

    /* Open a connection to the syslog server */
    openlog ("mediationd", LOG_PID, LOG_DAEMON); 
    syslog(LOG_NOTICE, "Successfully started daemon\n"); 

    /* Try to create our own process group */
    sid = setsid();
    if (sid < 0) {
        syslog(LOG_ERR, "Could not create process group\n");
        closelog();
        exit(EXIT_FAILURE);
    }

    /* Catch, ignore and handle signals */
    signal(SIGCHLD, SIG_IGN);
    signal(SIGHUP, SIG_IGN);

    /* Fork off for the second time*/
    pid = fork();

    /* An error occurred */
    if (pid < 0) {
        syslog(LOG_ERR, "Could not fork second time\n");
        closelog();
        exit(EXIT_FAILURE);
    }

    /* Success: Let the parent terminate */
    if (pid > 0) {
        exit(EXIT_SUCCESS);
    }

    /* Change the file mode mask */
    umask(0);       

    /* Change the current working directory */
    if ((chdir("/")) < 0) {
        syslog(LOG_ERR, "Could not change working directory to /\n");
        exit(EXIT_FAILURE);
    }

    /* Close the standard file descriptors */
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    /* Starting the perl script which does the job. 
    * Note, this perl scripts runs forever till it is terminated by 'kill' command */
    system(arg);

    /* Terminate the daemon */
    syslog (LOG_NOTICE, "Daemon terminated.");
    closelog();
    return EXIT_SUCCESS;
}

I wrote this tiny program by simple copy/paste, since I have no clue about C. Would be nice if someone could provide some "ready to use" code.

Update

I updated my code as shown below. It seems to work well, however do I miss anything or should I add something else?

/* Close the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);

/* Redirect STDERR to syslog, i.e. logger */
FILE *fl;
fl = popen("logger --id --priority user.error", "w");
if (fl == NULL) {
    syslog(LOG_ERR, "Could not open 'looger'\n");
    exit(EXIT_FAILURE);
}
int nf = fileno(fl);
dup2(nf, STDERR_FILENO);

/* Starting the perl script which does the job. 
* Note, this perl scripts runs forever till it is terminated by 'kill' command */
system(arg);
close(STDERR_FILENO);

/* Terminate the daemon */
syslog (LOG_NOTICE, "Daemon terminated\n");
closelog();
return EXIT_SUCCESS; 

Solution

  • If you want to do it in native Perl, this works for me:

    use IO::Handle;
    open(SYSLOG, "| /usr/bin/logger -t hello") or die( "syslog problem $!" );
    *STDERR = *SYSLOG;
    autoflush STDERR 1;
    

    If Perl crashes with die() or similar, you will see the log messages.