Logging to the System Logger in Bash
August 23, 2023 —
Gregg Szumowski
If you are writing a shell script and want any messages to appear in
the system logger you can just use the logger
command to do
this.
Here is an example on my Slackware system:
$ logger -t TEST "This is a test message"
$ cat /var/log/messages
cat: /var/log/messages: Permission denied
$ su -
Password:
# cat /var/log/messages
Aug 23 17:44:47 slackbook TEST: This is a test message
You can also log to systemd by providing a parameter to logger:
logger --journald <<end
MESSAGE_ID=67feb6ffbaf24c5cbec13c008dd72309
MESSAGE=The dogs bark, but the caravan goes on.
DOGS=bark
CARAVAN=goes on
end
or you can optionally provide a file whose contents will be logged:
logger --journald=entry.txt
See the logger manpage for additional information.