1

What I attempt to achieve is quite simple.

Whenever I connect a bluetooth device to my Rspberry Pi 3 (running Raspbian), a script shall add an entry to a logfile.

Simple it is, because the difficult stuff is already working fine.

I have already paired my phone and the following rule is triggered whenever it connects or disconnects:

pi@ras-pi:/ $ cat /etc/udev/rules.d/99-input.rules
SUBSYSTEM=="input",GROUP="input",MODE="0660"
KERNEL=="input[0-9]*",RUN+="/bin/bash -c 'echo TEST > /tmp/logfile.log'"

However, the result is always a non-existant logfile and a return code of 1:

pi@ras-pi:/ $ journalctl -xe -u systemd-udevd
Sep 05 12:54:09 ras-pi systemd-udevd[2333]: Process '/bin/bash -c 'echo TEST > /tmp/logfile.log'' failed with exit code 1.

At this point please note that this is a very simplified example.

Originally I had a standalone script which was successfully called and which was intended to write some information into the logfile using echo and output redirection.

I tried out various target directories and locations of the script for testing, but all with the same result (failed with exit code 1).

When I run the script in the current bash session it works fine as does the command mentioned at the top:

pi@ras-pi:/ $ /bin/bash -c 'echo TEST > /tmp/logfile.log'

When I remove the output redirection, so that it doesn't try to write to a file, I don't get the error message in the journal, so I guess the script works fine and it is just the redirection causing the problem. The same is the case for the original example:

KERNEL=="input[0-9]*",RUN+="/bin/bash -c 'echo TEST'"

Of course, my first guess was 'permissions', so this is the tmp directory:

pi@ras-pi:/ $ ls -la /
(...)
drwxrwxrwt  10 root root  4096 Sep  5 12:54 tmp
(...)

And this is the script I was executing (just to be complete):

pi@ras-pi:/ $ ls -la /usr/bin/bt_connect
-rwxr-xr-x 1 root root 110 Sep  5 11:36 /usr/bin/bt_connect

Oh and by the way: Adding sudo doesn't help:

pi@ras-pi:/ $ cat /etc/udev/rules.d/99-input.rules
SUBSYSTEM=="input",GROUP="input",MODE="0660"
KERNEL=="input[0-9]*",RUN+="/bin/bash -c 'sudo echo TEST > /tmp/logfile.log'"

Which results again in:

Sep 05 13:13:23 ras-pi systemd-udevd[2398]: Process '/bin/bash -c 'sudo echo TEST > /tmp/logfile.log'' failed with exit code 1.

Can someone please help me solve the problem ?

Update: At least I finally found a way to generate some output for debugging by writing not into a file, but to /dev/kmsg. This is just an example from my script:

echo $MSGCAT Path=$PATH >> /dev/kmsg

This way I found that the script is running under user 'root', but until now I didn't find out why the output to any file doesn't work.

  • Sorry for the missing formatting of the code sections. I added 4 spaces in front of all lines, but it doesn't seem to work... :-/ – Reinski Sep 5 '17 at 11:28
  • You have to add an extra empty line between the end of the text paragraph and the code. If not, the parser consider it is just one paragraph of text (beware, I left one line badly formatted, you can fix it yourself!). – perror Sep 5 '17 at 12:14
0

On Raspibian the systemd-udevd service most likely runs with the systemd parameter ProtectSystem. This means that udev will only be able to write to files in /dev directory. If you run

sudo systemctl edit systemd-udevd

it will open an empty (unless you've modified it before) text file in the system text editor (most likely nano) in which you'll be able to change the parameters of udev service. As per https://www.freedesktop.org/software/systemd/man/systemd.exec.html you might want to add

ReadWritePaths=/tmp

for your original script (writing to /tmp/logfile.log) to work. You will also need to restart udevd for changes to take effect

sudo systemctl restart systemd-udevd

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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