将 STDERR 从服务重定向到系统日志
Redirect STDERR from service to syslog
我必须 运行 Perl 脚本作为服务(即守护进程)。我在下面找到了 C 程序,总的来说它工作正常。但是,如果 perl 脚本异常终止,我不会收到任何错误消息。我怎样才能得到 perl 的错误?
终端(即 STDERR)不可用,我认为最好的办法是在 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;
}
我通过简单的 copy/paste 编写了这个小程序,因为我对 C 一无所知。如果有人可以提供一些 "ready to use" 代码就太好了。
更新
我更新了我的代码,如下所示。它似乎运行良好,但是我是否遗漏了什么或者我应该添加其他东西吗?
/* 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;
如果您想在本机 Perl 中执行此操作,这对我有用:
use IO::Handle;
open(SYSLOG, "| /usr/bin/logger -t hello") or die( "syslog problem $!" );
*STDERR = *SYSLOG;
autoflush STDERR 1;
如果 Perl 因 die()
或类似情况而崩溃,您将看到日志消息。
我必须 运行 Perl 脚本作为服务(即守护进程)。我在下面找到了 C 程序,总的来说它工作正常。但是,如果 perl 脚本异常终止,我不会收到任何错误消息。我怎样才能得到 perl 的错误?
终端(即 STDERR)不可用,我认为最好的办法是在 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;
}
我通过简单的 copy/paste 编写了这个小程序,因为我对 C 一无所知。如果有人可以提供一些 "ready to use" 代码就太好了。
更新
我更新了我的代码,如下所示。它似乎运行良好,但是我是否遗漏了什么或者我应该添加其他东西吗?
/* 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;
如果您想在本机 Perl 中执行此操作,这对我有用:
use IO::Handle;
open(SYSLOG, "| /usr/bin/logger -t hello") or die( "syslog problem $!" );
*STDERR = *SYSLOG;
autoflush STDERR 1;
如果 Perl 因 die()
或类似情况而崩溃,您将看到日志消息。