Desktop file failed to run as root user if specified in the advanced panel options of the property

Hello.
I have a desktop file which failed if user root is specified in advance option :

[Desktop Entry]
Comment[en_US]=test script good v1
Comment=test script good v1
Exec=IP_ADDR="192.168.130.239" ; while : ; do  ping -c2 "$IP_ADDR" ; if  $? -eq 0 ] ; then  kdialog --title="Connexion to $IP_ADDR" --msgbox="Router is alive" ; break ; else  kdialog --title "Connexion to $IP_ADDR" --warningcontinuecancel "No connection

Are you sure you want to continue pinging ?" ;   $? -eq 1 ] && break ; sleep 2 ;  fi  ;  done
GenericName[en_US]=test script good v1
GenericName=test script good v1
Icon=system-run
MimeType=
Name[en_US]=test script good v1
Name=test script good v1
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=true
X-KDE-Username=root

see :
https://paste.opensuse.org/7889809

Found error message in journald :

jan 31 19:22:43 15-3-G755VW-VM-INITIAL plasmashell[1876]: kf.service.services: Parsing "exist Exec and ('kdesu -u root -c /bin/sh -c 'IP_ADDR=\"192.168.130.239\" ; while : ; do  ping -c2 \"$IP_ADDR\" ; if  $? -eq 0 ] ; then  kdialog --title=\"Connexion to $IP_ADDR\" --msgbox=\"Router is alive\" ; break ; else  kdialog --title \"Connexion to $IP_ADDR\" --warningcontinuecancel \"No connection

Are you sure you want to continue pinging ?\" ;   $? -eq 1 ] && break ; sleep 2 ;  fi  ;  done '' =~ Exec)" gave: syntax error

Without setting user root it’s ok :

[Desktop Entry]
Comment[en_US]=test script good v1
Comment=test script good v1
Exec=IP_ADDR="192.168.130.239" ; while : ; do  ping -c2 "$IP_ADDR" ; if  $? -eq 0 ] ; then  kdialog --title="Connexion to $IP_ADDR" --msgbox="Router is alive" ; break ; else  kdialog --title "Connexion to $IP_ADDR" --warningcontinuecancel "No connection

Are you sure you want to continue pinging ?" ;   $? -eq 1 ] && break ; sleep 2 ;  fi  ;  done
GenericName[en_US]=test script good v1
GenericName=test script good v1
Icon=system-run
MimeType=
Name[en_US]=test script good v1
Name=test script good v1
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=

The exec command line is just an example.
What change should be done on the syntax, for using this desktop file as root or not ?

Any help is welcome

As far as I know, you cannot use a “.desktop” file to start an application as root.

What are you trying to accomplish?

Seems to be working, though. I made a small script checking if my server is there and then confirming it in a kdialog. (Took it from my startup sync script.) For a change I added “[FONT=courier new]cp /root/a/t /root/b/t” [/FONT]where t is a simple text file only accessible by root. Adding the X-KDE-Username=root apparently does the cp command in the restricted root folder. The permissions seem to be there. However I added kdialog --msgbox “I am user $USER” which doesn’t show “root” but “kasi”. You have to enter the root password, BTW.
As suggested in your other thread it really is advisable to put your code into the script. If find it easier than the “one-liner”. As far as I can see you want to check if your router is there? This is what I did:

#!/bin/bash
# this used to be my sync script


# kdialog --msgbox "hallo $USER"

# Send a ping to the server. Return 0 means it's there.
ping 192.168.130.239 -c 1

# assign it to variable servercheck
servercheck=$?

# Do the check:
if  $servercheck -eq 0 ] ; then
  #  confirm it's there:
    kdialog --msgbox  "The server is there." 
**  #  who is running this?
    kdialog --msgbox  "I am user $USER" 
  # Can I write in root's folder?
    cp /root/a/t /root/b/t**
      exit
 else
  # Exit: Better start the server
  kdialog --msgbox   "Have a look. Are you at home at all? The server is gone with return:  $servercheck"

  exit
fi

(The comments were there for my own memory, not for lecturing. Who am I? Just translated them rather than deleting.)
The bold lines just helped me to understand. I simply checked in dolphin if “t” is copied. (Will have to clean up root’s folder, now.)

FWIW, I should add the .desktop file:

[Desktop Entry]
Comment[de_DE]=
Comment=
Exec=/home/kasi/test/test.sh
GenericName[de_DE]=
GenericName=
Icon=system-run
MimeType=
Name[de_DE]=test
Name=test
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=true
X-KDE-Username=root

It’s very generic, created it by right-click on the desktop and select “create link to program” (if translation is correct). Interesting: “terminal=true” will give you more verbosity. I’d say this is the reason for not showing the user “root” but “kasi”:

QStandardPaths: runtime directory '/run/user/1000' is not owned by UID 0, but a directory permissions 0700 owned by UID 1000 GID 42

Thank you for answering.
But your example as nothing to do with mine.
You start a script from the exec key.

In my example, the command is in the exec key. That make all the difference.

I use a desktop file to start a script on a usb disk as root ( need to access /etc, /usr, /root, /home/user1, /home/user2 … on the current computer ) .

Now concerning the error, the system change the command ( escaping also the double quote ) like that :

kdesu -u root -c /bin/sh -c '   ***the key exec contents***  '

And that seems to generate a syntax error.

In konsole, Running the command

kdesu -u root -c /bin/sh -c '   ***the key exec contents***  '

run without error but message box title and text are not what is expected :
without ''
https://paste.opensuse.org/94221521

but get :
with ''
https://paste.opensuse.org/19031887

Any idea

It is NOT a command you have there. It is something that looks like a script to be interpreted by some not specified shell.

Maybe you should re-read https://specifications.freedesktop.org/desktop-entry-spec/latest/index.html#introduction
And specialy the chapter “The Exec key”, where it says rather at the begin

The Exec key must contain a command line. A command line consists of an executable program optionally followed by one or more arguments

Again, you will not appreciate this as “help”, but in my opinion I am trying to help you to understand the basics here. And without the basics: many problems.

Thank you, Noted

Yep, I got that. It’s just that your goal seems to be hard (possible at all?) to achieve. I know it’s tedious to ask “why” you want to do it. You might still share what you want to achieve by using a “one-liner” (as Henk said, it’s hardly a “command”) rather than starting a script. I can’t help there but maybe others can if they know what you are up to…

To run a script you must know where is it.
How do you know if the script running on the usb key is on /media/some/path/to/the/script, or /run/media/$USER/some/path/to/the/script, or if the usb disk in not mounted.

You got a point there. But I think one should be able to overcome that. Why not put the script itself on the desktop? Or, if that script needs to be “portable”, you can put another script on the desktop. That script can invoke the “mount” command:

**~ #** mount 
(...)
/dev/sdc1 on /run/media/kasi/**BIOS-FLASH** type vfat (...)
**pluto:~ #**

You can search for the label of your USB drive (in my case “BIOS-FLASH” I just got at hand) and there’s the path to your “real” script. That seems to be more practicable than the one liner in a “.desktop file”.