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:
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.
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:
home = os.environ["HOME"]
name = sys.argv; command = sys.argv
launcher = ["[Desktop Entry]", "Name=", "Exec=", "Type=Application", "X-GNOME-Autostart-enabled=true"]
dr = home+"/.config/autostart/"
if not os.path.exists(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
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