Linux – Centos syslog isn’t logging to custom file from bash script

bashcentos6linuxrsyslog

I'm trying to get a bash script to log to a custom log file using syslog on Centos release 6.5, but it always just logs to /var/log/messages like so:

Jan 12 09:54:47 localhost nbsp: Test message blah!

What am I doing wrong? Why isn't it logging to /var/log/my_prog_test.log? Here's are my configs and files.

/etc/rsyslog.conf (most of file omitted):

if $programname == 'my_prog_test' then {
   action(type="omfile" file="/var/log/my_prog_test.log")
}

After making the changes to rsyslog.conf I ran…

$ sudo /etc/rc.d/init.d/rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]

… so rsyslog was restarted after the config change.

The bash script /home/nbsp/bin/my_prog_test looks like this:

#!/bin/bash

logger 'Test message blah!'

The log file listing:

$ ls -la /var/log/ | grep my_prog_test
-rw-------.  1 root root      0 Jan 11 09:23 my_prog_test.log

The symlink…

$ ls -la /usr/bin/my_prog_test
lrwxrwxrwx. 1 root root 29 Jan 12 08:54 /usr/bin/my_prog_test -> /home/nbsp/bin/my_prog_test

The program's listing:

$ ls -la bin/my_prog_test
-rwxrwxr-x. 1 nbsp nbsp 50 Jan 12 09:09 bin/my_prog_test

Best Answer

Your script is calling logger(1) to write something to syslog. Thus rsyslog will see the program name "logger" doing the write and not "my_prog_test".

For what you want to achieve call logger(1) with "-p local0.notice" and in rsyslog.conf filter by the facility "local0" instead of program name "my_prog_test"..

According to the documentation for rsyslog on my system (Debian Wheezy with rsyslog 7.4.4 from backports) the latter would then be expressed as:

  if $syslogfacility-text == 'local0' then {
     action(type="omfile" file="/var/log/my_prog_test.log")
  }

This should also work and would be simpler for this case:

  local0.* /var/log/my_prog_test.log

If on your system the syslog facility local0 already gets used for such a purpose, you can instead use local1 up to local7 instead.

Related Topic