How to add a script to Startup Applications from the command line?

How to set up a startup launcher from command line

Like you mention in your question, commands can be run on log in by placing a launcher in ~/.config/autostart Since the launcher is only used to startup a script, you only need the "basic" desktop entry keywords in the created .desktop files: the keywords / lines you'd need at least:

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

The (optional) line X-GNOME-Autostart-enabled=true will be added automatically if you enable/disable the autostart function of the launcher (it is set to X-GNOME-Autostart-enabled=true by default)

More on required fields, you can find here.

Example script

To create such a launcher from the command line, you would need a small script that would take the name of the starter and the command to run as an argument. An example of such a script below.

If I run it with the command:

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

It creates a startup launcher, running gedit when I login.
The launcher is also visible in Dash > Startup Applications:

enter image description here

The script

#!/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")

Paste it into an empty file, save it as set_startupscript.py, run it by the command:

python3 /path/to/set_startupscript.py '<name>' '<command>'

What it does

  • It creates a basic launcher (you don't need more, running a script) in ~/.config/autostart, taking the name and command as arguments.
  • If a launcher with the name already exists in ~/.config/autostart, it prints a message:

    file exists, choose another name
    

I found an answer

cd to ~/.config/autostart/. If you don'y have a folder named autostart then create one with that name using mkdir autostart.

Now add the following file with the name 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

Done!