How to create a systemd service with custom error messages?

In this thread I try to create a rather complex service.

The service has 2-3 ExecStartPre lines that try to do something, which is evaluated in sh -c "Command". If the command is false, the service should cancel and repeat after a given time to check again.

The service doesnt output any usable error messages and I wonder how to do that.

I tried

ExecStartPre=<check for stuff> || sh -c 'echo "Error: check failed" >&2'

To echo it to stderr.

Anyone knowledgeable here?

Personally, I would move all the executable code to a script.
This should make it easier to maintain and troubleshoot.

1 Like

The || is a shell operation, not something systemd understands right?

I second putting all the logic into a script that you execstart.

1 Like

Yes you are right, will do that tomorrow

And any idea how I could make the script give exit codes that systemd will see?

if [ "${BATTERY}" -lt "40" ]
then
    echo "${MESSAGE}" >&2
    exit "${CODE}"
fi
1 Like

thanks! I did a pretty big overhaul and put everything into a way better script.

The systemd stuff is now very minimal, while the script handles the logic. The timer deals with the planned running action (every day at 20:00, if not met then as soon as possible min 10min after boot), the service repeats itself when not ran successfully (need to check if that works)

2 Likes