How to automatically run a script when logging into Ubuntu Desktop

Last updated on September 8, 2020 by Dan Nanni

There are circumstances where you wish to have a script run automatically when you log in to Ubuntu Desktop. Such a script can configure various user-specific or system-wide settings on your Ubuntu system, upon user's desktop login.

In Linux, there are start-up scripts named ~/.bash_profile, ~/.bashrc, or ~/.profile which get executed when you start a shell. However, in Ubuntu Desktop, such start-up scripts get executed when you open up a terminal window, but not when you log in to Ubuntu Desktop GUI. Also, when you open multiple terminal windows, these kinds of start-up scripts are executed as many times, in order to initialize user's shell environment in terminal windows.

If what you want is to run a script at the time of user's Ubuntu Desktop login, you can follow this guideline.

Create a XDG configuration file for the start-up script you want to run.

$ vi ~/.config/autostart/my_script.desktop
[Desktop Entry]
Type=Application
Name=My Script
Exec=~/bin/my_custom_script.sh
Icon=system-run
X-GNOME-Autostart-enabled=true

The above XDG configuration file will set up user-specific auto-start. If you want all users to use the same start-up script for all users system-wide, create a similar XDG configuration file in the following location instead.

$ sudo vi /etc/xdg/autostart/my_script.desktop
[Desktop Entry]
Type=Application
Name=My Script
Exec=sudo /sbin/my_custom_script.sh
Icon=system-run
X-GNOME-Autostart-enabled=true

If the start-up script requires sudo access like an above example, you will need to set up password-less sudo. Refer to this tutorial for setting up auto-start services in different types of Linux desktop environments (e.g., GNOME, KDE, Xfce, LXDE).

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean