Announcement

Collapse
No announcement yet.

HOW-TO run a QT app from ROOT as a NON-ROOT target user

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    HOW-TO run a QT app from ROOT as a NON-ROOT target user

    I am summarizing again the scenario/requirement, solution and explanation so that if anyone wants to run a QT (KDE graphical) program from root as a non-root target user this should cover most of the cases on how to do it. Obviously YMMV depending on your distro, version, packages installed, etc. but it should give you a great head start.

    SCENARIO/REQUIREMENT:

    For various reasons you might want to run a QT program launched by ROOT for a NON-root target user.
    Example: nvidia proprietary drivers cause graphical glitches on some apps after you wake up from suspend-to-ram. For some of them the solution is to restart the app (like for a dock). However you need a /usr/lib/systemd/systemd-sleep hook script file in order to do that, as the restart must occur only and when the machine wakes up. These scripts are always ran as ROOT. But the QT app you want to restart must be ran as a NON-ROOT user.

    SOLUTION:

    Create a resume script, executable from root and place it in your proper systemd-sleep folder, something like this
    Code:
    /usr/lib/systemd/system-sleep/resume.sh
    The command to run on wake up is the following:
    Code:
    su - $user -c "KDE_FULL_SESSION=true XDG_RUNTIME_DIR=/run/user/1000 DISPLAY=:0 nohup $programtorestart 2>&1 > /dev/null &"
    where
    Code:
    "$user" is your non-root target user you want this to run for
    and
    Code:
    "$programtorestart" is the QT app you want to run
    The
    Code:
    nohup ... 2>&1 > /dev/null &
    is not mandatory, it's just cleaner cuz it sends everything from stderr to stdout.

    A full example of this resume script is the following:

    Code:
    #!/bin/bash
    if [ "${1}" == "pre" ]; then
    # Run on sleep
    #nothing to run
    exit 0
    elif [ "${1}" == "post" ]; then
    # Run on resume
    sleep 2
    pkill -TERM latte-dock
    sleep 2
    su - frank -c "KDE_FULL_SESSION=true XDG_RUNTIME_DIR=/run/user/1000 DISPLAY=:0 nohup latte-dock 2>&1 > /dev/null &" 
    exit 0
    fi
    More in details to understand:

    When running from root it is disconnected from the non-root user's display, XDG runtime environment and KDE session parms. All these variables must be set to your command in order to tell root which conditions it must be ran (run?) into.

    So XDG_RUNTIME_DIR is required to set in order to tell root to use the user's "1000" folder which is used for XDG stuff.
    Then DISPLAY is required in order to tell root to open up that target user's display to draw the app in there.
    And finally KDE_FULL_SESSION is required in order to tell root to use the full KDE session's parms of the target user.

    You can test your command by invoking a "sudo -i" session and there you have it.

    Alternatively, if you want to permanently set these environment variables system-wide across all users (including root) so they are set all the time, you can edit
    Code:
    /etc/environment
    and add the following 3 lines
    Code:
    KDE_FULL_SESSION=true
    XDG_RUNTIME_DIR=/run/user/1000
    DISPLAY=:0
    Therefore you can omit them in your command, but be aware they will always be set for any command you launch no matter what your user is.
    Last edited by FrankKubuntu; Mar 05, 2020, 08:02 PM.
    Frank
    Kubuntu 19.10/20.04
    AMD Ryzen 7 3700x
    Gigabyte X570 AORUS ELITE
Working...
X