0

I created a simple oneshoot systemd service file on a Raspberry Pi which starts up on boot and logs a specific journal namespace and some dmesg locally to a file.

Executing the script locally as a test seems to work, but the automatic start has issues and does not work at all.

test.service

[Unit]
Description=Test message
After=multi-user.target

[Service]
Type=oneshot
ExecStart=/data/test.sh
StandardOutput=journal

[Install]
WantedBy=multi-user.target

The system is rather simple, there is not much going on. Only root user is there.

One temperature logger, which logs to journal and has a LogNamespace=temp_logger also starts with the system. I did not put it in the dependencies or somewhere else.

Now when I execute the file test.sh with this content:

#!/bin/bash

echo $(date)
TEST_DIR=/data/testdirectory

mkdir -p $(TEST_DIR)

journalctl -f --namespace=temp_logger > ${TEST_DIR}/journaltemp.log &

dmesg -w > ${TEST_DIR}/dmesg.log &


It seems to work fine. No errors, nothing. Looks good. If I enable the service with systemctl enable test and reboot the system there seem to be some issues.

The directory didnt get created and the log files are not there. (I deleted the ones from the test)

When checking with systemctl status test I can see error messages that tell me, the command echo, mkdir etc. were not found.

When I modify all calls from echo or mkdir etc. to /bin/echo and /bin/mkdir etc., it seems to work. I get the echo and the date and the directory. I checked all calls to binaries with the whichcommand and adapted the paths.

However, the journal log seems not to work. The files are there but have no content (size 0)

What exactly goes wrong there? Why do I need to give the absolute path to binaries ? Why does the journalctl not work?

6
  • Did you also change to /usr/bin/journalctl? I suggest, as a test, you also /usr/bin/echo ${PATH} to a file. It may be helpful.
    – Bib
    Oct 16, 2021 at 11:21
  • @Bib yes i made all the paths absolute, but why is this even necessary? Oct 16, 2021 at 20:33
  • Did changing to /usr/bin/journalctl work then? The echoing of PATH to a file is based on an assumption that the var is not setup yet.
    – Bib
    Oct 16, 2021 at 21:36
  • It did not help changing the journalctl to absolute path. The directory gets created, the log files are there, but are empty. Do you know why the PATH is like this? Do I need to state some user or start time dependency on the service file? Oct 17, 2021 at 9:35
  • Sigh, I don't know what the PATH is like as you will not tell us.
    – Bib
    Oct 17, 2021 at 10:14

1 Answer 1

0

Turns out the service had that of a simple dependency, that it started up before the journal service itself was up.

Thats probably why the files were there, but kept being size 0. lsof did not show me any process for the files dmesg.log or journaltemp.log

I ended up by just putting a good ol' sleep 5 at the beginning of the file test.sh and it works since then.

There probably is a better / cleaner solution by setting to start after the journald in the service file, but I could not figure that out. The sleep works and now is part of my test.sh

1
  • Wouldn't adding systemd-journald.service to After achieve what you want?
    – frippe
    Oct 18, 2021 at 19:28

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.