this post was submitted on 09 Jun 2026
10 points (100.0% liked)

Linux Questions

4162 readers
11 users here now

Linux questions Rules (in addition of the Lemmy.zip rules)

Tips for giving and receiving help

Any rule violations will result in disciplinary actions

founded 3 years ago
MODERATORS
 

I start a bash shell script via a oneshot unit. I output standard messages currently with

echo $message
echo $message >> $logfile.txt

With this I have output on screen and additionally in a logfile. I don't know if there is a better way to do that.

This works for standard infos fine. Additionally there must be a way to output also error messages and warnings, since when I look into other services, I see red messages for errors and yellow/orange colored messages for warnings.

How can I do such things with my bash script and output other levels like warnings in yellow and errors in red?

top 3 comments
sorted by: hot top controversial new old
[–] Jackinopolis@sh.itjust.works 2 points 3 months ago (1 children)

It's been a minute since I've looked into logging. Syslog is the OG standard, but generally improved with rsyslog and journald. You'll have logging levels with these and can be colored/filtered in a viewing program.

[–] MoLoPoLY@discuss.tchncs.de 2 points 3 months ago

Many thx for the hint with journald. A little bit searching gives me https://serverfault.com/questions/573946/how-can-i-send-a-message-to-the-systemd-journal-from-the-command-line

I try that tomorrow. From reading it seems to be, what I need.

[–] Chais@sh.itjust.works 2 points 3 months ago* (last edited 3 months ago)

AFAIK the general approach to logging with systemd is that the application simply writes to stdout/stderr and systemd takes care of passing the output to whatever logging target/s you configured.
As for signaling log levels with bash, you can write functions that prepend any timestamps, markers or wrap the string in any color codes you want.