How to run a program every time a user logs in to Linux (and where the startup configuration files are stored)

Sometimes new Linux users want to know how to run a particular command or shell script every time a user logs in (which also implies that if it’s a single-user system, this could be a command that runs when the system starts). In Ubuntu and its many variants, you can simply place the startup command in Ubuntu’s Startup Applications Preferences, as we demonstrated in the article, Enabling a SOCKS proxy via SSH tunnel in Ubuntu or Mac OS X at startup.

Ubuntu Startup Applications Preferences - adding SOCKS proxy
Ubuntu Startup Applications Preferences – adding SOCKS proxy

However, you may be on a variant of Linux that doesn’t have a Startup Applications Preferences program, or maybe you simply want to add something from the command prompt. Or maybe you want to tweak an existing startup command to do something different. The key to this is that the startup configuration files for each user are stored in the ~/.config/autostart directory (where ~ is the user’s home directory). The actual format of the files may differ between different variants of Linux, and some versions may not utilize them at all, but if that directory exists on your system, that’s probably where they are stored. There are also system-wide autostart files; those are found in the /etc/xdg/autostart directory, at least on Ubuntu-based systems.

For example, referring back to the command to activate a SOCKS proxy when Ubuntu starts (the first example in the article referenced above), what happens when you create that command in the Startup Applications Preferences is that it creates a file named ~/.config/autostart/screen.desktop (the filename will be different for each autostart command), which in turn contains the actual script that’s run at startup (similar to this example):

[Desktop Entry]
Type=Application
Exec=screen -dmS tunnel ssh username@server_address -D local_socks_proxy_port
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=SSH Tunnel
Name=SSH Tunnel
Comment[en_US]=SSH SOCKS proxy tunnel
Comment=SSH SOCKS proxy tunnel

The startup scripts can contain much more than just this. When you install certain programs that autostart, they will write considerably more complicated configurations. In Ubuntu, you can take a look at the /etc/xdg/autostart/update-notifier.desktop file as an example of a more complex configuration file.

Possibly the best way to figure out the format of these files is to take a look at several of them in the directories mentioned above, since they may well differ to some degree in different versions of Linux. There are other ways to start programs at startup in Linux, but they may run at system startup before a user has logged in, or they may only execute if a user logs in using a terminal session, but not when they bring up a desktop session. The method shown here is supposed to work no matter how a user logs into the system. And, it can sometimes be useful to know where Linux stores its configurations.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

Recent Comments

Archives

Categories

Meta

GiottoPress by Enrique Chavez