3

Hello in the sudo systemctl status <service> I sometimes see an error in text like [info] asio async_shutdown error: asio.system:32 (Broken pipe), yet it is still marked as [info] and status of service is active (running).

I need to monitor the service log and if there is a word error in the log I need to issue sudo systemctl restart <service>. Is that possible?

5
  • this is definitely possible. there are a number of log watchers for linux out there that you can use, or even just use tail in a chron job like tail -f some-logfile | awk '/some-pattern/ {system("run-some-command")}' . it will require some scripting, but is entirely possible. Commented Feb 2, 2022 at 18:07
  • Thanks I tried this and it did not do anything. I tried to add second sudo but it did not help: sudo journalctl -f -u ifi-streamer-tidal-connect.service | awk '/Max connection attempts/ {system("systemctl restart ifi-streamer-tidal-connect.service")}'
    – Jan
    Commented Feb 3, 2022 at 15:07
  • have you verified that the journalctl command is producing output that awk can parse? Commented Feb 3, 2022 at 17:02
  • Yes for example this is the line that should trigger service restart: Feb 03 15:04:54 tidal-pi tidal_connect_application[21553]: [2022-02-03 15:04:54.239] [tisoc] [warning] [logger.cpp:22] [audio_worker.cpp:288] Max connection attempts reached!
    – Jan
    Commented Feb 3, 2022 at 20:16
  • I believe the problem is that Journalctl is buffering output, since it doesn't exit, so the pipe is never getting the input. try adding the --no-pager option to your journalctl command. Commented Feb 3, 2022 at 21:44

1 Answer 1

1

I solved it by running following command every 5 seconds in a loop:

journalctl -n 10 -u ifi-streamer-tidal-connect.service | grep -i "Max connection attempts" && sudo systemctl restart ifi-streamer-tidal-connect.service
2
  • glad you got it working. I'm a little surprised that grep has a different exit code for when it found somthing than when it didn't. that's neat! you can replace your loop with a cron job if you don't want to manually restart the script after reboots. that will also help if the command crashes every once in a while. you may want to watch to make sure it doesn't react to the same log entry more than once though. Commented Feb 3, 2022 at 23:20
  • 1
    I avoid the duplicate run by limiting number of lines. Restart produces about 20 lines of log, so I am sure that after restart there is no error message anymore.
    – Jan
    Commented Feb 4, 2022 at 9:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .