0

I have a program which is started by systemd service myprogram.service. Problem is that it is writing its logs directly to the log file /opt/myprogram/logs.txt. I want to redirect its logs into systemd journal, so I will be able to read them with journalctl.

Is it possible to amend myprogram.service to tell Systemd to pipeline text from specific file into its journal?

3
  • Is the application able to instead write its output to stdout? Some applications do that when run with a --no-daemon (or similar) flag.
    – Wieland
    Apr 2, 2022 at 10:53
  • Or a --syslog switch.
    – Bib
    Apr 2, 2022 at 12:47
  • @Wieland Problem with that particular application is that it is hardcoded to write logs into file. That is why I'm trying to pipeline it.
    – anlar
    Apr 3, 2022 at 18:22

1 Answer 1

0

As a start, I would try a combination of tail and logger.

tail -f /opt/myprogram/logs.txt | logger

tail will continuously read all new lines from the file and duplicate them in stdout. And logger will take those lines and send them to system log.

EDIT:

Since the piping is doing buffering there is a chance that the logger will sleep for some time (until the default buffer of 4Kb fills out). That can be remedied by disabling buffering with stdbuf or unbuffer:

stdbuf -i0 -o0 tail -f /opt/myprogram/logs.txt | logger
4
  • I have tried to add ExecStartPost=/bin/sh -c 'tail -F /opt/myprogram/data/log.txt | systemd-cat' to systemd service. It works but as tail should be left running when I'm trying to start service it is fail with timeout, because systemd waits for tail to exit.
    – anlar
    Apr 3, 2022 at 18:24
  • Disable pipe bufering. Add unbuffer or stdbuf -i0 -o0 to the tail. For example: stdbuf -i0 -o0 tail -f ...
    – White Owl
    Apr 3, 2022 at 22:05
  • It fails with the same error # systemctl start myprogram Job for myprogram.service failed because a timeout was exceeded.. Systemd waits until tail returns exit code so buffering shouldn't help with it.
    – anlar
    Apr 4, 2022 at 16:56
  • I think you are mixing something. The suggested tail+logger solution works for sure in the user mode. If you starting it from systemd.service.... it could be a cause of the error. Not sure how exactly, but 'systemctl' in the error message points in that direction. Try running tail+logger from a script which starts your application, of if the app itself is running by systemd - just add a log copy script to startup (or just run it manually).
    – White Owl
    Apr 5, 2022 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.