1

I am currently using the following to read the system log:

journalctl -u <service name> | tail -n1

However I have a need to monitor the system log live, as changes come in (in a bash script), instead of just looping through the log file.

What is the best way to do this? I did some research to where the journalctl command is reading from, and it seems that the system logs are unreadable (or at least when I attempted with cat.

Any suggestions would be much appreciated!

Reboot
  • 11
  • 1

1 Answers1

0

journalctl tool has a -f flag which enables printing the contents of log file as soon as it is changed. Use it like this:

$ journalctl -u <service name> -f

nkrivenko
  • 1,231
  • 3
  • 14
  • 23
  • Thanks! Will this work in a script though? I imagine that I would need some way of constantly reading that input. That is where I am stuck I have iterated a loop so far with a sleep, but this is not catching every new line. – Reboot Nov 01 '20 at 19:43
  • Could you explain what do you mean by "work in a script"? You can use it like this: `$ journalctl -u -f | grep ` and grep will work on each appended line in your log file – nkrivenko Nov 01 '20 at 19:55
  • For clarity, my end goal is to continually read the entries from the system log file, process them through a loop, and continue to do so until the script is terminated. So I would need to do something like ` -f | grep
    read case...`
    – Reboot Nov 01 '20 at 20:04
  • I believe it can work for you like this, for example: `journalctl -u -f | grep
    | ./process-through-loop.sh`
    – nkrivenko Nov 01 '20 at 20:26
  • That makes sense, however I believe my requirements include this logic being inside of the script, instead of running it from a journal command. If I was to go with your suggested route, would I just use a `while read line` to iterate through each incoming line? Do you have any idea if I could implement the function of doing a journalctl -f inside a bash script? – Reboot Nov 01 '20 at 20:40