I have a udev rule that starts an application via a shell script. When the udev rule is called twice in a row and as soon as I close the application, it is started again. How can I prevent this?
I want to run this script when I press a button on by stylus. I connects via bluetooth when the button is pressed. The script shall run the app, when the app is not running or maximize and focus it, when it is running. Problem is that I can only call the script once and the udev events are queueing up when I press a second time. As soon as I close the app, it restarts due to the queued up event. I also tried putting xournalpp &
, but then the app is killed as soon as the udev rule terminates I assume. I have posted in multiple forums. No one seems to know a solution. I have tried nohup
and flock -n
. They all work when I call the script from bash, but not when it is called from the udev rule.
My best guess for now would be to use flock on the call of xournalpp in the script and then prevent udev somehow from queueing up events. Or maybe use a systemd unit that processes the udev rule, so the app doesn’t get terminated then using xournalpp &
. I have no idea if that is possible, but I read that by using a unit, it doesn’t terminate.
ACTION=="add" \
, SUBSYSTEM=="bluetooth" \
, ATTRS{idProduct}=="0002"\
, ATTRS{idVendor}=="1d6b"\
, ENV{DISPLAY}=":0" \
, ENV{HOME}="/home/user/" \
, RUN+="/home/user/runXournalpp"
#!/bin/bash
if pgrep xournalpp
then
echo Xournal++ is running
wmctrl -x -a com.github.xournalpp.xournalpp
wmctrl -x -r com.github.xournalpp.xournalpp -b "add,maximized_vert,maximized_horz"
else
echo Xournal++ is not running
xournalpp
fi