20

I have a shell script. I want to configure my script to run automatically during startup. I know how to do this from GUI. But I wanted to do this from terminal. How can I do this?

On a primary research I found that the file needs to be moved to /etc/int.d/ directory. But this operation needs sudo permission. I wanted to do this without super user permissions.

I also found that there are files under ~/.config/autostart/ which are having some settings regarding the startup applications. But I don't know how to edit them to achieve this.

Can someone tell me the exact procedure to achieve this?

  • 1
    Do you need to have it run on user level or globally? Also: you could create a launcher in ~/.config/autostart from command line, but it would take... a minor script :). Would that be ok? – Jacob Vlijm Mar 18 '15 at 9:19
  • User level shall do. I am also trying to write a script. But unaware of the files which are needed to be edited. :) – Anonymous Platypus Mar 18 '15 at 9:21
19

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
    
  • @AnonymousPlatypus aaarrgh, forgot the import sys line. fixed. – Jacob Vlijm Mar 18 '15 at 9:52
  • @Jacob What should you put in the place of <name> and <command>? – TellMeWhy Jun 25 '15 at 13:47
  • 1
    @DevRobot <name> is the name of your Startup Application, as it appears in the list of Dash > Startup Applications. You can give it any name you like. The <command> is the command you'd like to run. If it is simply a bash script, use: '/bin/bash /path/to/script.sh', (including quotes) if it is a complicated command, use /bin/bash -c "<complicated_command>". What is the command you'd like to run on startup? – Jacob Vlijm Jun 25 '15 at 13:53
  • @Jacob, it's a bash script containing randr settings – TellMeWhy Jun 25 '15 at 13:58
  • @DevRobot then the first option will do (don't forget the quotes :) ) – Jacob Vlijm Jun 25 '15 at 14:00
6

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!

  • 1
    To run a script on startup, there are more fields here than needed :) – Jacob Vlijm Mar 18 '15 at 9:49
  • Could you please tell me about the additional fields? – Anonymous Platypus Mar 18 '15 at 9:55
  • 2
    only a few of these fields are really required, if you look at this site: standards.freedesktop.org/desktop-entry-spec/latest/… in column REQ, you can see if you really need the keyword. I would only use additional keywords if you have a reason to. Nodisplay, Hidden, Comment etc. play no role when you don't run launchers from Dash. – Jacob Vlijm Mar 18 '15 at 9:59
  • 1
    I have checked the file. As you said, according to that only Type,EXEC and X-GNOME-Autostart-enabled tags would be required. Thanks for pointing this out. :) – Anonymous Platypus Mar 18 '15 at 10:08
  • 2
    You are right! If the line is not in the .desktop file, the default value is true, if you disable it from Dash > Startup Applications, the line is added automatically. I edited it into the script. – Jacob Vlijm Mar 19 '15 at 5:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.