You are not logged in.

#1 2024-06-24 15:59:15

Registered: 2019-07-21
Posts: 218

[SOLVED] Can't create auto-run service that forks script to background

I've followed … process.3F
and created a file
/etc/systemd/system/mmp.service :

Description=Mountain MacroPad driver

and the file /usr/local/bin/ :


# run Mountain MacroPad driver

cd /home/millus/mountain-macropad/
/bin/sh -c 'nohup ./mmp &'

Now when I run this file (as root) manually everything works fine, the keys work and "ps" shows that mmp is running.
However, if I run it (as root) via "systemctl start" :

# systemctl start mmp
# systemctl status mmp
○ mmp.service - Mountain MacroPad driver
     Loaded: loaded (/etc/systemd/system/mmp.service; enabled; preset: disabled)
     Active: inactive (dead) since Mon 2024-06-24 17:53:55 CEST; 5s ago
   Duration: 3ms
    Process: 4169876 ExecStart=/usr/local/bin/ (code=exited, status=0/SUCCESS)
   Main PID: 4169876 (code=exited, status=0/SUCCESS)
        CPU: 4ms

Jun 24 17:53:55 archlinux systemd[1]: Started Mountain MacroPad driver.
Jun 24 17:53:55 archlinux systemd[1]: mmp.service: Deactivated successfully.

...except the mmp executable is not run or at least not successfully completed, as a "ps" command afterwards will show nothing (and the macropad keys don't work, which they do if I just run the script manually).
I can add another command to the script, eg echo "hi" > test, and it is executed fine, so it's not that the whole script isn't working, just the mmp call doesn't succeed somehow.

The mmp binary reads /dev/usb/hiddev* in the background and generates keypresses via xdotool, while printing debug log output to stdout, which we can discard.
The nohup & stuff is necessary to make it run independantly from any terminal.

So the question basically is, why does work fine if I run it manually, but not if I use the "systemctl start" command.

Ok, I got it to actually run mmp process by adding
to the "[Service]" block. It seems that systemd tracks all of the forked/backgrounded processes you create in your script and just kills them all when the main script exits o_o. So this is needed for them to be left alive.
However, it seems that the process isn't "working" as the macropad keys are not generated. So the mmp file is somehow called but also frozen in some way. Still works fine if just started manually.

Aha, as soon as I trigger keys on the pad, I get errors in the journal. First:

Jun 24 18:35:45 archlinux[4172489]: Error: DISPLAY environment variable is empty. Is there an Xorg or other X server running? You can try setting 'export DISPLAY=:0' and trying again.
Jun 24 18:35:45 archlinux[4172489]: Failed creating new xdo instance.

so I added
export DISPLAY=:0.0 to the script. Now instead I get these:

Jun 24 18:36:18 archlinux[4172528]: Authorization required, but no authorization protocol specified
Jun 24 18:36:18 archlinux[4172528]: Failed creating new xdo instance.

So I had now idea what kind of auth it was talking about, but it seems it means ".Xauthority" files. Now my root user didn't have one, so since this script is running as root maybe that caused it. So I added
export XAUTHORITY=/home/millus/.Xauthority
to the script, as my user does have this file apparently, and.. it works! (Probably not for other hypothetical users now, if this Xauthority thing somehow matters, no idea).

Had all this stuff actually failed my two final resorts would have been to just have the service file script create a screen session in the background where this runs, eg
screen -d -m -S mmp-screen -m /usr/local/bin/mmp
or try a cron @reboot thing for the root user's cron table...

Ugh, I had to add a "sleep 5" before the mmp call in the script, because this script will (at least on my system) execute faster than udev rules are applied, so the macro pad will not yet be ready as usb-hiddev device and mmp will fail to find it and terminate.

Link to complete Mountain Macropad thread where I incorporated everything from this thread now ->

Last edited by millus (2024-06-24 17:25:16)


Board footer

Powered by FluxBB