brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: logger -t lets a script write its own lines to the system log

Linux
~/tips/tip-shell-logger-tag-syslog

logger -t backup "nightly sync finished"

Writes a tagged message from a script to the system log so you can find it later.

A script that only prints to the terminal leaves nothing behind when it runs from cron at night. logger sends a line to the system log instead.

The logger man page says it makes entries in the system log. With a message argument, that message is written to the log. If there is no message and no -f file, standard input is logged. The -t option tags every line, and without it the tag defaults to the name of the user:

logger -t backup "nightly sync finished"

On a system with journald you can then find your own lines by tag with journalctl -t backup, which filters by syslog identifier.

The priority is user.notice by default. Use -p with a facility and level pair, such as local3.info, to change it. For an error, I would write:

logger -t backup -p user.err "rsync failed with code $?"

Two more options are handy in scripts. -s also prints the message to standard error, in the same form as it is sent to syslog, so a terminal run shows what the log gets. -f file logs the contents of a file, and the manual says it cannot be combined with a message on the command line. Piping works as well, because standard input is logged when no message is given:

rsync -a /mnt/user/appdata/<app>/ /mnt/backup/ 2>&1 | logger -t backup

The caveat is that a log line without context is easy to lose in a busy log. I would always give a fixed tag, so the lines can be found again, and keep the messages short. Also note that where the lines end up depends on the machine and its logging setup, so look once with journalctl -t or in your log files instead of assuming.

Further reading: logger(1) manual page, journalctl manual: -t, –identifier

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link