首页 > 解决方案 > syslog 程序无法写入 /var/log/messages 文件

问题描述

编写系统日志消息的程序。

#include <stdio.h>
#include <unistd.h>
#include <syslog.h>

int main(void) {
    openlog("slog", LOG_PID|LOG_CONS, LOG_USER);
    syslog(LOG_INFO, "A different kind of Hello world ... ");
    closelog();
    return 0;
}

标签: rsyslog

解决方案


为我工作

% cat > syslog.c
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>

int main(void) {
    openlog("slog", LOG_PID|LOG_CONS, LOG_USER);
    syslog(LOG_INFO, "A different kind of Hello world ... ");
    closelog();
    return 0;
}

% cc syslog.c
% ./a.out
% tail /var/log/messages
tail: cannot open ‘/var/log/messages’ for reading: Permission denied

% su
Password: 
[root@build-centos7 src]# tail /var/log/messages | grep slog
Sep 17 13:09:10 build-centos7 slog[3040]: A different kind of Hello world ...

推荐阅读