0

I am running an Arch Linux installation and I have a systemd service that runs /etc/rc.local. In rc.local, I have several commands to perform things that I couldn't find a suitable solution for, like running dmesg -D or something that would automatically create mount points in a temporary folder as in macOS with /Volumes.

For each command in my rc.local script, I have the script test for a failure and then exit with a number corresponding to the line of the command. So, a snippet of the file (adjusted to keep my formatting in mind) would look like this:

#!/bin/bash

dmesg -D || exit 1
i=2
for name in usb sd
do
    mkdir -p /mnt/${name} || exit ${i}
    i=$((i+1))
done
sysctl -w kernel.kptr_restrict=2 || exit 4

exit 0

This is a bit clunky and I'm under the impression that exit codes should be limited in their assignment for a script, but this is the only way I know how to check what the reason for my systemd service rc-local.service failing is since systemctl status rc-local.service will print out the exit value. Is there some other way to code rc.local such that systemd/rc-local.service will receive information on why rc.local fails, like maybe passing a string to it or something? Then I can see this string show up maybe like this when I type systemctl status rc-local.service:

Error reason: Failed to set kernel.kptr_restrict
1

Failures (non-zero exit codes) should cause the command (or the shell, if the command crashed) to print an error.

systemd will write error messages like this to the system log. And the nice thing about systemctl status is that it displays the service's most recent log lines.

http://0pointer.net/blog/projects/systemctl-journal.html

EDIT: Reading the system log is only possible when systemctl status is run as root (sudo), or if you are a member of the group adm or systemd-journal.

  • But will systemctl status rc-local.service write any error messages printed by /etc/rc.local? – Melab Aug 15 '17 at 19:09
  • @Melad I tried an edit, hopefully this is clearer. – sourcejedi Aug 15 '17 at 21:09

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.