35

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?

αғsнιη
  • 35,660
  • 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 09: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 09:21

2 Answers2

34

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
    
dessert
  • 39,982
Jacob Vlijm
  • 83,767
  • @AnonymousPlatypus aaarrgh, forgot the import sys line. fixed. – Jacob Vlijm Mar 18 '15 at 09:52
  • @Jacob What should you put in the place of and ? – 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
  • @Jacob Last question ;) I'm using python to write the above python script into a file, but the \n in the code creates a new line in the file, it isn't copied as \n, so I get an EOL error. How can I solve this? – TellMeWhy Jun 25 '15 at 14:18
  • @DevRobot You mean integrate it in an existing script? Then I would fit it in a function. For easy maintenance you could keep it as a separate module and either import it, or call it by subprocess.call() or subprocess.Popen(). Is it python2 or 3? For more to the point looking into it, probably better to post a question. – Jacob Vlijm Jun 25 '15 at 14:35
  • The script is very handy - it's going in my toolbox. :-) – Nigel Atkinson Aug 02 '16 at 00:31
13

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!

  • 3
    To run a script on startup, there are more fields here than needed :) – Jacob Vlijm Mar 18 '15 at 09:49
  • Could you please tell me about the additional fields? – Anonymous Platypus Mar 18 '15 at 09:55
  • 2
    only a few of these fields are really required, if you look at this site: http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s05.html 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 09: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
  • Wait, X-GNOME-Autostart-enabled ? That is not needed, and is not even mentioned on the linked page. – Jacob Vlijm Mar 18 '15 at 12:48
  • I believe the X-GNOME-Autostart-enabled key tell it to actually start the program, if it were set to false then it would appear disabled in the list of startup programs. – Anonymous Platypus Mar 19 '15 at 04:49
  • 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 05:22