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.