.desktop 文件在 centos8 上不可执行

.desktop files are not executable on centos8

我用 centos 7 从 python 脚本创建了一个 .desktop 文件(我在 ssh 桌面文件中远程部署),这个快捷方式执行另一个 python 脚本。 OS 已经升级到centos 8.

不幸的是,我的桌面条目不再对用户有效。

2个问题:

-对于用户,不显示图标(文件不受信任,用户需要信任文件才能显示图标)。 我尝试执行: gio set myShortcup.desktop metadata::trusted 是的 它似乎工作正常,但用户必须单击 f5 才能刷新桌面)

-当用户双击 .desktop 文件时,带有桌面输入代码的文本编辑器正在打开: 但对我来说,在远程终端上使用 ./myShortcut.desktop 命令启动了应用程序)。 用户必须右键单击文件并使其可执行才能解决问题,并且它仅在桌面文件夹中有效(不能直接在桌面中)。

拜托,我如何远程解决这两个问题并生成这些快捷方式?

生成代码:

#!/usr/bin/env python2

import os, sys, logging
import glob
import os.path
import datetime

# DECLARATION
desktopPath         = "/home/user/Desktop/"
scriptPath          = "/home/user/Desktop/DEPLOY/"

# METHODS
def createShortcut(fileName):

        # File name
        shortcutName = fileName.replace(".raw","") + ".desktop"

        # Remove file if exists
        try:
                os.remove(desktopPath + shortcutName)
        except OSError:
                pass

        with open(desktopPath + shortcutName, "w") as shortcut:
                shortcut.write("[Desktop Entry]\n")
                shortcut.write("Name=" + fileName.replace(".raw","") +"\n")
                shortcut.write("Exec=python2 " + scriptPath +"myApp.py -f " + fileName + "\n")
                shortcut.write("Terminal=true\n")
                shortcut.write("Type=Application\n")
                shortcut.write("Icon="+ scriptPath + "icon.png\n")

        shortcut.close()

        # Add execution permission
        try:
                os.chmod(desktopPath + shortcutName, 0o777)
        except OSError:
                pass


# MAIN CODE 

def main(argv):

        # Set logging level
        logging.basicConfig(stream=sys.stderr, level=logging.INFO)

        # Loop raw files
        fileList = glob.glob('*.raw')

        for i in fileList:

                # Create shortcut on PC desktop
                fileName = i
                createShortcut(fileName)



if __name__ == "__main__":
        main(sys.argv)

已创建文件:

[Desktop Entry]
Name=myShortcut
Exec=python2 /home/user/Desktop/DEPLOY/myApp.py -f myShortcut.raw
Terminal=true
Type=Application
Icon=/home/user/Desktop/DEPLOY/icon.png

谢谢

我在家里用virtualbox的centos 8.2.2004虚拟机上测试过,重现了这个问题,我无法执行脚本,也无法右键单击"allow launching"。

经过几个小时的调查,要解决这些问题,您需要将 chmod 777 编辑为 775(可能是 linux security...selinux?我不知道具体原因) 而且你还需要添加 gio metadata trusted 命令(不要注意我的错误缩进):

        # Add execution permission
        try:
        os.chmod(desktopPath + shortcutName, 0o755)
        cmd="gio set "+ desktopPath+shortcutName + " \"metadata::trusted\" true"
        os.system(cmd)
        except OSError:
                pass

在那之后,它奏效了。在您的机器上进行测试并告诉我们它是否解决了问题。