In Linux desktop system, I want to execute a command when the user logs in.

After reading some other posts, I tried to insert the command in ~/.bashrc but unsuccessfully. Moreover, the system uses a graphic interface for the user login, so the command should not be related to the start of a shell.

I also tried to append the command to one of the scripts contained in /etc/profile.d with no results.

Is there another way to do this? Any file that the system reads after the login?


3 Answers 3


There is no guarantee that the graphical display manager will read the classic startup files. This changes between distributions and between display managers. One of the following should work though.

  1. Use your desktop environment's native method to set startup applications. The details will depend on the DE you are using, but you can create a script that runs your command and add it to the list of startup applications. For example, on my system (Cinnamon), you can do this through "System Settings" => "Startup Applications".

  2. Use ~/.xprofile, this is sourced by at least the GDM, LDM, LightDM and LXDM login managers.

  3. If neither of the above work, try adding the command to ~/.profile : This is the main initialization file for login shells and is also read by some graphical shells on login.

  4. As @derobert pointed out in the comments, you can also use the free desktop standards:

    The Autostart Directories are $XDG_CONFIG_DIRS/autostart as defined in accordance with the "Referencing this specification" section in the "desktop base directory specification".

    If the same filename is located under multiple Autostart Directories only the file under the most important directory should be used.

    Example: If $XDG_CONFIG_HOME is not set the Autostart Directory in the user's home directory is ~/.config/autostart/

    Example: If $XDG_CONFIG_DIRS is not set the system wide Autostart Directory is /etc/xdg/autostart/

    Example: If $XDG_CONFIG_HOME and $XDG_CONFIG_DIRS are not set and the two files /etc/xdg/autostart/foo.desktop and ~/.config/autostart/foo.desktop exist then only the file ~/.config/autostart/foo.desktop will be used because ~/.config/autostart/ is more important than /etc/xdg/autostart/

The ~/.bashrc is completely irrelevant here, it is only read by interactive non-login shells, so is ignored on login shells, graphical or not.

  • 5
    Might want to mention ~/.config/autostart and /etc/xdg/autostart/ which is are the standard locations according to the Desktop Application Autostart Specification. And also /etc/X11/Xsession.d and ~/.xsession.
    – derobert
    Commented Mar 31, 2014 at 16:17

Another option is to use pam - this will give you a precise way to define on-login actions.

For a generic action you can rely on pam_exec (http://manpages.ubuntu.com/manpages/hardy/man8/pam_exec.8.html). However, if you need to perform a more specific action in a safe way, there may exist more specialized pam modules which will make a better fit, such as commonly used pam_mount (for on-login mounts - http://manpages.ubuntu.com/manpages/hardy/man8/pam_mount.8.html) or pam_echo (for arbitrary messages to users - http://manpages.ubuntu.com/manpages/hardy/man8/pam_echo.8.html).

In general, pam is a very neat system for customizing logins, so you may want to look a bit more into it, instead of relying on potentially unsafe scripting, as others had suggested.


Given a fairly typical /etc/pam.d/system-auth we can employ pam_exec after login like this:

session         optional        pam_ssh.so
session         required        pam_limits.so
session         required        pam_env.so
session         optional        pam_mktemp.so
session         required        pam_unix.so
session         optional        pam_exec.so /usr/local/bin/my_prog
session         optional        pam_permit.so

where /usr/local/bin/my_prog is arbitrary program being run after a successful user login.

  • 2
    This is a good answer, though you should include an example of the line that you would add the the pam configuration since this is non-trivial.
    – Graeme
    Commented Apr 1, 2014 at 1:00

According to this topic: Run command automatically after login?

You have the solution of the .bashrc (not what you need) and the solution of startup applications. I quote Daniel S.:

gnome-session-properties can be used to configure startup applications.

Also, if you want an application to run at system boot, you can add a rule like the following to your crontab (edit crontab with crontab -e):

@reboot /run/this/program/at/boot >/dev/null 2>&1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .