Add Startup script from CLI
From: https://askubuntu.com/questions/598195/how-to-add-a-script-to-startup-applications-from-the-command-line





How to set up a startup launcher from command line

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/init.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.

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, 2015 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. :)



Answer 1 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 '' ''
What it does 39.5k1212 gold badges116116 silver badges164164 bronze badges answered Mar 18, 2015 at 9:44 82.7k1212 gold badges195195 silver badges300300 bronze badges @AnonymousPlatypus aaarrgh, forgot the import sys line. fixed. – Jacob Vlijm Mar 18, 2015 at 9:52 @Jacob What should you put in the place of and ? – TellMeWhy Jun 25, 2015 at 13:47 1 @DevRobot 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 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 "". What is the command you'd like to run on startup? – Jacob Vlijm Jun 25, 2015 at 13:53 @Jacob, it's a bash script containing randr settings – TellMeWhy Jun 25, 2015 at 13:58 @DevRobot then the first option will do (don't forget the quotes :) ) – Jacob Vlijm Jun 25, 2015 at 14:00 =======================================================
Answer 2 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 :)– Could you please tell me about the additional fields? – Anonymous Platypus Mar 18, 2015 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, 2015 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, 2015 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, 2015 at 5:22 =======================================================
Localized values for keys Keys with type localestring and iconstring may be postfixed by [LOCALE], where LOCALE is the locale type of the entry. LOCALE must be of the form lang_COUNTRY.ENCODING@MODIFIER, where _COUNTRY, .ENCODING, and @MODIFIER may be omitted. If a postfixed key occurs, the same key must be also present without the postfix. When reading in the desktop entry file, the value of the key is selected by matching the current POSIX locale for the LC_MESSAGES category against the LOCALE postfixes of all occurrences of the key, with the .ENCODING part stripped. The matching is done as follows. If LC_MESSAGES is of the form lang_COUNTRY.ENCODING@MODIFIER, then it will match a key of the form lang_COUNTRY@MODIFIER. If such a key does not exist, it will attempt to match lang_COUNTRY followed by lang@MODIFIER. Then, a match against lang by itself will be attempted. Finally, if no matching key is found the required key without a locale specified is used. The encoding from the LC_MESSAGES value is ignored when matching. If LC_MESSAGES does not have a MODIFIER field, then no key with a modifier will be matched. Similarly, if LC_MESSAGES does not have a COUNTRY field, then no key with a country specified will be matched. If LC_MESSAGES just has a lang field, then it will do a straight match to a key with a similar value. The following table lists possible matches of various LC_MESSAGES values in the order in which they are matched. Note that the ENCODING field isn't shown. Table 1. Locale Matching
LC_MESSAGES value Possible keys in order of matching
lang_COUNTRY@MODIFIERlang_COUNTRY@MODIFIER, lang_COUNTRY, lang@MODIFIER, lang, default value
lang_COUNTRYlang_COUNTRY, lang, default value
lang@MODIFIERlang@MODIFIER, lang, default value
langlang, default value
For example, if the current value of the LC_MESSAGES category is sr_YU@Latn and the desktop file includes: Name=Foo Name[sr_YU]=... Name[sr@Latn]=... Name[sr]=... then the value of the Name keyed by sr_YU is used. Although icon names of type iconstring are localizable, they are not human-readable strings, so should typically not be handled by translation tools. Most applications are not expected to localize their icons; exceptions might include icons containing text or culture-specific symbology.