1

I am writing a bash script and am checking whether the application is running. If it is not running it should be started in a separate process (not a child process). If it is running, the window should be maximized. I kind of made it but the new process terminates shortly after being started, probably because the script process ends. When I don't run it in background via &, the app doesn't close, but then I can't run the script a second time to maxmize the app.

#!/bin/bash
if pgrep app
then
    echo app is running
    # Maximize the app
    wmctrl -x -r WMClassOfapp -b "add,maximized_vert,maximized_horz"
else
    echo app is not running
    /usr/bin/app 
fi

When I execute it in a terminal, it works fine. When I call it from the udev rule, the app terminates. Using nohup yields the same outcome. Here is the udev rule

ACTION=="add" \
, KERNEL=="hci0:3585" \
, SUBSYSTEM=="bluetooth" \
, ENV{DISPLAY}=":0" \
, ENV{HOME}="/home/user"
, RUN+="/home/user/runapp"
6
  • Can you post the udev rule you used to start the service?
    – AdminBee
    Sep 24, 2020 at 9:06
  • Just added the rules file. I actually don't want to use systemd, because that seems a bit too hacky to me. A rule that calls a service that calls a script...
    – neolith
    Sep 24, 2020 at 9:09
  • Does it run for just a little time when using the systemd approach? Note that the oneshot type should be used with RemainAfterExit=true, otherwise it will never reach the active state, but directly transition to dead. The problem with starting a script from udev is that udev is now part of systemd anyway, and running a script from a udev rule may block processing of further udev events. systemd on the other hand mercilessly kills all processes and sub-processes started by a service when that service has ended ...
    – AdminBee
    Sep 24, 2020 at 9:13
  • No it doesn't run at all. I just tried RemainAfterExit=true but the same happens. So when udev is a part of systemd anyway, I should not need systemd. I just want to start this app and keep it running. Like I said, it works when I don't run it in background in the script, but it needs to start in a separate process. Else I can't call the script a second time and maximize the app.
    – neolith
    Sep 24, 2020 at 9:18
  • Is the runApp in your ExecStart line a typo? In your udev rule it was runapp in all-lowercase ...
    – AdminBee
    Sep 24, 2020 at 9:24

1 Answer 1

0

I found a workaround here at last. You need a second script that contains

#!/bin/bash
echo /pathto/runApp | at now

This script has to be called from the udev rule. For it to work at has to be installed and you have to enable atd by calling sudo systemctl enable atd --now. It is a little hacky, but at least it works.

I deleted the systemd approach so the post will be an easier guide.

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.