HostOnNet Blog

How to automatically run a script when logging into Ubuntu Desktop

Looking for Linux Server Admin or WordPress Expert? We can help.

Ubuntu_10.10_remote_desktop_windows

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. Then you want all users to use the same start-up script “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.

About sherly

I am Sherly, living in the city of Alappuzha, which known as the 'Venice of the East'.
Posted in Linux, Ubuntu

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.