LXDE: Autostart Apps With .desktop Files

by Paul Arnote (parnote)

Under almost every other major desktop environment, it's a relatively simple task to set up applications to automatically start whenever you start the desktop. However, this is not necessarily so under LXDE. In fact, LXDE does not natively have an autostart directory, by default. Thanks to Neal Brooks, author of the PCLinuxOS-LXDE remaster, PCLinuxOS users of LXDE do have this feature already set up for them.

pic

Hidden in the user's home directory, is the .config folder. Under the .config folder, you will find a folder named autostart. Just as with KDE, items placed in the autostart directory will be automatically started when LXDE starts.

You might think it to be as easy as placing a link — either a symbolic link or a hard link — to the application you want to automatically start in the autostart directory. But that is not going to work. Nope. Only actual .desktop files work to automatically start the selected applications when LXDE is started. And no, you cannot create a link to the .desktop files. It has to be an actual, bona fide .desktop file.

Of course, the easiest way to obtain the proper .desktop file is to, (as root), copy the appropriate .desktop file from /usr/share/applications to the /$HOME/.config/autostart directory. But what if the application you want to automatically start doesn't have a corresponding .desktop file? Read on.

Uses for the .desktop file

Before we discuss how to create the .desktop file for the application(s) you wish to automatically start, it's important to understand how .desktop files are used on your system. Obviously, one such use is the main topic of this article: automatically starting applications when you start LXDE. But probably one of the primary uses of .desktop files is to display items in your LXDE menu.

All items displayed in the LXDE menu have a corresponding .desktop file in /usr/share/applications. So much of the information here can also be utilized to customize your LXDE menu. In this aspect, LXDE is not all that unlike Xfce, and the information in the Xfce 4.6.2: Customize Your Xfce Menu article (June 2010 issue of The NEW PCLinuxOS Magazine) will also apply to your efforts to customize your LXDE menu.

Fortunately, LXDE follows the standards for the .desktop file set forth by freedesktop.org. This link will take you to the page that explains all the recognized "keys" in a compliant .desktop file, as well as specifying whether each key is required or optional. While not an "official" standards organization, the "guidelines" set forth by Freedesktop.org have become defacto standards.

Creating the .desktop file

Just as with many things in Linux, there is more than one way to create a .desktop file for the application you wish to automatically start when LXDE starts. A .desktop file is, as many files in Linux are, a simple text file. Using the .desktop file for AlsaMixer GUI as an example, here is the basic format for that .desktop file:

[Desktop Entry]
Name=AlsaMixerGUIComment=Advanced Linux Sound Architecture (ALSA) graphical mixer
Exec=alsamixergui
Icon=sound_section
Terminal=false
Type=Application
Categories=Audio;Mixer;X-MandrivaLinux-Multimedia-Sound

The lines should be fairly self-explanatory, but here's a brief rundown on each. The "Name" entry is, as you might expect, the name displayed for the application. The "Comment" entry contains the information that is supplied when a user hovers their mouse over the entry in the LXDE menu. The "Exec" entry specifies the application to launch. Although most of the executable files on your computer are stored in the /usr/bin folder, it would be wise to specify the full path to the application. If your application, indeed, has its executable file stored in /usr/bin, you can get away with specifying only the application's executable name, because /usr/bin is in your path. Otherwise, you will need to specify the full path to the application's executable file.

The "Icon" entry specifies the icon to display for the specified application. The "Terminal" entry specifies if the application should be opened in a terminal session. The "Type" entry most likely doesn't need an explanation. Finally, the "Categories" entry specifies, first, the categories that the application should be classified as, and second, where to place the application's icon in the LXDE menu. In our example above, that would be under the LXDE > Multimedia > Sound menu.

So now that you have a basic understanding of what a .desktop file does, and how it's constructed, it's time to discover how to create our .desktop file. The first choice, and probably the most obvious one, is to simply create the file by hand, in a basic text editor such as Leafpad. To prevent the program from appearing in the LXDE menu, refrain from saving the .desktop file to your /usr/share/applications directory. Conversely, if you want the program to appear in your LXDE menu, be sure to save the .desktop file (or a copy of it) in your /usr/share/applications directory. You will need root privileges to save the file there. For setting up an application to automatically start when you start LXDE, be sure to save the .desktop file (or a copy of it) to your /$HOME/.config/autostart directory.

The second choice is to take an existing .desktop file on your system and modify it for your needs. Open up an existing .desktop file, make the changes to the listed keys, and resave it with the same name as your application, but with the .desktop extension.

The third choice is to follow the steps outlined in the Xfce 4.6.2: Customize Your Xfce Menu article (June, 2010 issue of The NEW PCLinuxOS Magazine) by running the exo-desktop-item-edit command to create your .desktop file with a GUI.

Regardless of the method you choose to use, don't forget to save the resulting .desktop file (or a copy of it) in your $HOME/.config/autostart folder for those applications you want to automatically start when you start LXDE.

Advanced: A Workaround (Easier) Shortcut

Now, I'm calling this "advanced," but don't interpret advanced as meaning difficult. Actually, this workaround is easier. I call it "advanced" for two reasons. First, it involves making a simple bash script. This fact alone may keep some of you from attempting it. Second, it's a different way of looking at the problem, and offers a different solution that is more flexible.

This method started off as a "proof of concept" idea in my head. I don't know if anyone has tried this before. If so, great. If not, I wonder why. So let me walk you through this method, step-by-step.

Step One: Create a simple bash script, similar to the following:

#!/bin/bash

sleep 10
dropbox &
conky &
aumix &
leafpad &
pcmanfm &
checkgmail &
pcc &

All I've done here is simply list all the the applications I want to automatically start when LXDE starts. The first line causes a 10 second delay in the execution of the rest of the script. This delay allows the desktop to finish loading before I start launching applications. Notice that each application name is followed by a space, then the ampersand sign. The ampersand tells bash to execute the application in the background, and move on to the next line. Without the ampersand, the script would first launch dropbox, wait for it to finish and exit, and once finished, launch conky. Once conky was finished executing and exits, then aumix would launch. Things would proceed in this manner until all the applications listed had been executed, one at a time.

This list assumes, of course, that I want to launch DropBox, Conky, Aumix, Leafpad, PCManFM, CheckGmail and PCC every time I start my computer. I am certain that I do not want all of these applications automatically started when I start LXDE, as a matter of fact. But I list them here to prove that this technique works, validating my proof of concept. You can just as easily list other applications here that you may want to automatically launch whenever LXDE starts.

Step Two: Save your new bash script. I called mine autostart-lxde.sh. Sure, you can call it whatever you like. But I have this thing about making the names mean something that makes sense to me six months or a year down the line. I saved my bash script in my $HOME directory.

Step Three: Right click on your new bash script, and select "Properties" from the context menu.

pic

Click on the "Permissions" tab, and check all the boxes that are labeled "Execute." This will allow not only the file owner to run the script, but also all members of the specified group, as well as all other users on the system.

Step Four: Right click on your script, and select "Copy" from the context menu. Open up the /usr/bin directory (as root), and paste your script into that directory. The /usr/bin directory is most desirable, since it is in your $PATH. When you paste a copy of your script in the /usr/bin directory, root will become the owner and group of the script. An added benefit is that the list of applications to automatically start when LXDE starts can only be changed or edited by a user with root privileges.

If you want to make it possible for any user to edit the script (or easier for YOU to edit the script), you can save it to some other directory of your choice. If you choose this route, you will have to provide the entire path to the script in the .desktop file that you create in the next step.

Step Five: Create a .desktop file for your script. It should look something like the following:

[Desktop Entry]
Name=LXDE Autostart
Comment=Automatically start listed applications when LXDE starts.
Exec=autostart-lxde.sh
Icon=/usr/share/icons/5.png
Terminal=false
Type=Application
Categories=Configuration

Save the file to your $HOME/.config/autostart directory. There is no need to save this .desktop file to your /usr/share/applications directory, since its sole purpose is to automatically launch your selected applications when LXDE starts. In fact, I don't have all the proper parameters set up in the example .desktop file above for the script to even appear in your LXDE menu.

Now, when you start LXDE, all the applications listed in your script will be launched automatically. To test it, log out of your current LXDE session, and then log back in. If you've followed all the directions accurately, all the applications listed in your script should automatically start when you start LXDE.

Remember that I said this method is easier and is more flexible? It certainly cleans up your $HOME/.config/autostart directory. Instead of having a lot of .desktop files filling up the autostart directory, you now only have one (or two, since DropBox places one there automatically for us) that replaces them all. It also saves space on your hard drive. Instead of having multiple copies of the .desktop files repeated in your autostart directory, there's only one. Finally, it's more flexible. You can automatically start any application on your system with this method, regardless if it has a .desktop file or not.

Also, even though I haven't tried it extensively, this method should work equally well on just about any other desktop environment. As long as it has a provision for automatically starting applications when the desktop environment starts, there should be no problem, since .desktop files are generally seen as being executable files.

Can the script be improved upon? I'm certain of it, since my scripting skills are very, very basic. But as it exists in its current state, it's very functional. It just works. I'm sure that for someone (hint, hint) who is good with creating scripts with a GUI interface (either via Zenity or Gtkdialog), it would be a fairly simple proposition to create a GUI script to help create the autostart-lxde.sh script and .desktop file. This would give PCLinuxOS-LXDE users something that no other users of LXDE on other Linux distros have: a graphical way to manage the applications to automatically start when LXDE starts.