60

I have a script that I need to start up after the user logs in via a nx client. From what I understand, Gnome starts up after a user logs in (correct me if I am wrong here). Where would I put my script so that it starts up after the user logs in?

Relevant Information: I am working on a redhat machine and assume that I have access only to the files within the home directory (the ~ directory) and not the / directory (can someone tell me what that directory is called? the / one). Are there any files that I can modify/add in the home directory that will help me start up my script? What about if I wanted to start the script from / directory?

Just to clarify since it might've not been clear. The ~/home directory implies all files in the home directory including the ones in the subdirectories; same with the / directory.

Jason Zhu
  • 2,194
  • 3
  • 23
  • 27
  • 1
    The `/` directory is usually called the `root` directory, which can often be confused with `/root` or `/home/root` which is the directory where the root user's files and configuration live. – dragon788 Sep 08 '17 at 14:34

7 Answers7

116

There is a specification for this purpose, and it works with any desktop environment that supports the specification (eg. KDE, GNOME2, GNOME3, ...).

You should put a Desktop Entry file (with .desktop extension) inside of ~/.config/autostart (for a user) or /etc/xdg/autostart (for the whole system). In the file you set the script to run (see the option Exec). For more information, check the Desktop Application Autostart Specification.

For example, you can put a file with the following content:

[Desktop Entry]
Name=MyScript
GenericName=A descriptive name
Comment=Some description about your script
Exec=/path/to/my/script.sh
Terminal=false
Type=Application
X-GNOME-Autostart-enabled=true

You can also use the graphical user interface to create one entry or Add a new entry:

  • in GNOME 2 is System/Preferences/Startup Applications.
  • in GNOME 3, press Alt+R and look for Startup Applications Preferences (typing the first letters of the name will filter it quickly) or run gnome-session-properties from there or a terminal.
gpoo
  • 8,408
  • 3
  • 38
  • 53
  • 2
    If you need the "working directory" (cd/pwd) to be somewhere specific, you can use the Path key to specify that. eg, `Path=/opt/CoolBeans/` https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.1.html#exec-variables – Ray Foss Mar 31 '16 at 12:13
  • 1
    And with the [`OnlyShowIn` or `NotShowIn` keys](https://specifications.freedesktop.org/autostart-spec/autostart-spec-latest.html), you can target your startup script to a specific X desktop. – palswim May 03 '16 at 06:24
  • The file name of the file containing the desktop entry should have an extension ".desktop" – JSixface Sep 28 '16 at 19:23
  • 3
    The `Exec` key always seems to add `~/` in front of any path to a file. For example, `Exec=gedit ~/myfile` will open a (new) file named `~/~/myfile` and `Exec=~/myscript` or `Exec=/home/$USER/myscript` will fail whereas `Exec=./myscript` will succeed. I've seen this recently in GNOME, Cinnamon, and Xfce environments (in Arch, btw). This seems strange to me and doesn't seem to be part of the spec. Is this always the case for other distros/environments, too? – theDrake Feb 25 '21 at 03:53
25

In gnome3, if you prefer a GUI based approach you could run the command: ALT + F2, then type in the window that pop ups:

gnome-session-properties
ernesto
  • 351
  • 3
  • 2
  • Gnome-session-properties adds entries to your ~/.config/autostart so I don't believe it is actually deprecated. – dragon788 Sep 08 '17 at 14:21
6

In Gnome3, you'd have to create a folder ~/.config/autostart and put the script in there

abhinav
  • 3,199
  • 2
  • 21
  • 25
5

There's no handy tool to do it in GNOME 3, so you can create a .desktop file manually or use this app.

hant0508
  • 528
  • 1
  • 9
  • 19
3

GNOME Ships an Application called "GNOME Tweak Tool" or simply called "Tweaks". This does have an section called "Startup Applications".

After you added something to autostart, i recommend to you too look in ~/.config/autostart what this tool did.

-1

Must add:

Exec=**xterm -e** ~/skripts/my_script

`xterm -e' → command args ...  command to execute 
Sinval
  • 1,315
  • 1
  • 16
  • 25
-1

Here is the official help documentation for gnome3.

Install gnome-tweaks, then:

  1. Open the Activities overview and start typing Tweaks.
  2. Click Tweaks to open the application.
  3. Click the Startup Applications tab.
  4. Click the + button to get a list of available applications.
  5. Click Add to add an application of your choice to the list.

https://help.gnome.org/users/gnome-help/stable/shell-apps-auto-start.html.en

cupen
  • 157
  • 1
  • 7