如何从命令行向启动应用程序添加脚本?

我有一个shell脚本。 我想将我的脚本配置为在启动期间自动运行。 我知道如何从GUI执行此操作。 但我想从终端做到这一点。 我怎样才能做到这一点?

在初步研究中,我发现需要将文件移动到/etc/int.d/目录。 但是这个操作需要sudo权限。 我想在没有超级用户权限的情况下这样做。

我还发现~/.config/autostart/下的文件有一些关于启动应用程序的设置。 但我不知道如何编辑它们来实现这一目标。

有人能告诉我实现这个目的的确切程序吗?

如何从命令行设置启动启动器

就像你在问题中提到的那样,可以通过在~/.config/autostart放置一个启动器来登录时运行命令由于启动器仅用于启动脚本,因此您只需要创建的“基本”桌面条目关键字.desktop文件:至少需要的关键字/行:

 [Desktop Entry] Name=name Exec=command Type=Application 

如果启用/禁用启动程序的自动启动function(默认情况下设置为X-GNOME-Autostart-enabled=true ),将自动添加(可选)行X-GNOME-Autostart-enabled=true

有关必填字段的更多信息,请点击此处 。

示例脚本

要从命令行创建这样的启动程序,您需要一个小脚本,该脚本将以启动程序的名称和命令作为参数运行。 下面这样一个脚本的一个例子。

如果我用命令运行它:

 python3 '/path/to/script' 'Test' 'gedit' 

它创建了一个启动启动器,在我登录时运行gedit
启动器在Dash>启动应用程序中也可见:

在此处输入图像描述

剧本

 #!/usr/bin/env python3 import os import sys home = os.environ["HOME"] name = sys.argv[1]; command = sys.argv[2] launcher = ["[Desktop Entry]", "Name=", "Exec=", "Type=Application", "X-GNOME-Autostart-enabled=true"] dr = home+"/.config/autostart/" if not os.path.exists(dr): os.makedirs(dr) file = dr+name.lower()+".desktop" if not os.path.exists(file): with open(file, "wt") as out: for l in launcher: l = l+name if l == "Name=" else l l = l+command if l == "Exec=" else l out.write(l+"\n") else: print("file exists, choose another name") 

将其粘贴到一个空文件中,将其保存为set_startupscript.py ,然后通过以下命令运行它:

 python3 /path/to/set_startupscript.py '' '' 

它能做什么

  • 它在~/.config/autostart创建一个基本的启动程序(您不需要更多,运行脚本),将名称和命令作为参数。
  • 如果~/.config/autostart已存在具有该名称的~/.config/autostart ,则会打印一条消息:

     file exists, choose another name 

我找到了答案

cd到~/.config/autostart/ 。 如果您没有名为autostart的文件夹,请使用mkdir autostart创建一个名称为autostart的文件夹。

现在添加名为yourScript.sh.desktop的以下文件

 [Desktop Entry] Type=Application Exec="/Your/location/to/theScript/yourScript.sh" Hidden=false NoDisplay=false X-GNOME-Autostart-enabled=true Name[en_IN]=AnyNameYouWish Name=AnyNameYouWish Comment[en_IN]=AnyComment Comment=AnyComment 

完成!