Distributions adhering to the X-Desktop specifications will autostart applications from a system wide directory XDG_CONFIG_DIRS/autostart, and from an autostart directory in the user's home. Only the latter will be accessible without root permissions.
Define an autostart application in HOME
1. Find autostart directory
We first need to know where out autostart directory resides. According to the X-Desktop specifications the location should be as follows:
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/
In a standard Ubuntu installation $XDG_CONFIG_HOME is not defined. The autostart directory therefore resides in ~/.config/autostart. Any .desktop file located in this directory will start the corresponding application on login.
2. Create the autostart directory if not present
Before we can populate the autostart directory we need to check for its presence, and eventually have to create it.
3. Define .dektop file
For Unity and GNOME a minimum .desktop file will have the following content to enable autostarting an application:
[Desktop Entry]
Type=Application
Exec=</path/to/application>
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=<name of application>
We may distribute a template .desktop file with our application to copy it to or delete it from the autostart directory depending on user options set.
Define a system wide autostart application
To enable an autostarting application for all users we need to populate the system wide autostart directory (e.g. /etc/xdg/autostart) with our .desktop file.
To be able to do this from user options we need to temporarily allow root permissions. This can e.g. be done by calling a subprocess with pkexec from our settings dialog.
See also this related question: