Skip to content

Instantly share code, notes, and snippets.

@Ragdata
Forked from jclosure/logging_to_syslog.md
Created August 23, 2024 22:25
Show Gist options
  • Save Ragdata/63718ed5fe20bdd94a71cb948dd61a83 to your computer and use it in GitHub Desktop.
Save Ragdata/63718ed5fe20bdd94a71cb948dd61a83 to your computer and use it in GitHub Desktop.
Logging to syslog from bash and using dedicated custom log files that get rotated automatically

demo by creating a script called run.sh

#!/bin/bash

# https://urbanautomaton.com/blog/2014/09/09/redirecting-bash-script-output-to-syslog/
exec 1> >(logger -s -t $(basename $0) 2>&1)

for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done

verify it logs by tailing the main syslog file:

tail -f /var/log/syslog

log to a custom file by adding a filter to the rsyslogd config

edit /etc/rsyslog.d/50-default.conf

at the bottom add:

if $programname == 'run.sh' then /var/log/run.sh.log
& stop
sudo service rsyslog restart

set the log file up to be rotated

edit /etc/logrotate.d/rsyslog

at the bottom add:

/var/log/run.sh.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment