Hack 72. Start Desktop Applications Automatically
Start tools, utilities, and applications automatically as soon as you start your favorite desktop or window manager. You might want to start a number of programs automatically as soon as you launch your window manager or desktop environment. For example, assume you set up your LinEAK keyboard utility so that you can use all the special keys on your Internet and multimedia keyboard [Hack #29] . Naturally, you want to run this utility every time you start your window manager, but you don't want to have to start it manually. What you need is a way to have LinEAK and any other program you want running launch automatically as you start up your graphical desktop. Some of the solutions that follow are features of the X Window System, so they will work regardless of the window manager or desktop you use. But many window managers and desktops also have their own special and interesting ways to start up programs automatically, so this hack covers those as well. 9.4.1. Back to BasicsOne of the most basic ways of starting a window manager is to create a file called ~/.xinitrc, and set it up so that it runs your favorite window manager whenever you issue the command startx. Assuming you use this approach, here's the simplest form of the .xinitrc file using Fluxbox as an example window manager to launch: exec /usr/bin/fluxbox All this does is start up the Fluxbox window manager. To start LinEAK, the custom keyboard utility, add the following line to the .xinitrc file before you start Fluxbox: lineakd & exec /usr/bin/fluxbox Notice the ampersand (&) following lineakd, which tells lineakd to start in the background. Unless a program places itself automatically in the background without a trailing ampersand, you need to make sure you add the ampersand. Without the ampersand, the lineakd command will not release control back to the ~/.xinitrc script, and Fluxbox will never start. You can launch many additional commands via ~/.xinitrc, including commands that control how your X11 graphical system behaves. For example, the xset -b command turns off beeps in terminal windows. You can also use xset to set the volume and duration of beeps, control the lights (LEDs) on your keyboard, and much more. In this case, you don't need an ampersand after the xset commands, because xset returns control to the script on its own. Here is a slightly more advanced .xinitrc file: xset -b xcompmgr -cCfF -l 0 -t 0 -r 5 -o .6 & lineakd & exec /usr/bin/fluxbox This script also includes the xcompmgr command [Hack #33] (which works only with Xorg-X11 6.8 or better). This command sets window drop shadows and fade effects. Unlike xset, the xcompmgr program needs an ampersand to be told to run in the background. 9.4.2. Using the Window Manager and Desktop FeaturesWindow managers and desktop environments often provide their own means of starting programs automatically. The rest of this hack covers specific window managers and how they automatically run programs. 9.4.2.1 WindowMakerWindowMaker uses a script file to start programs automatically. The script file is ~/GNUstep/Library/WindowMaker/autostart. You can edit the ~/GNUstep/Library/WindowMaker/autostart file to include commands much like the ones you would place in your .xinitrc file. For example, put this commented command in the autostart file to launch the utility that enables your Internet/multimedia keyboard: # XOSD-enabled internet/multimedia keyboard utility lineakd & 9.4.2.2 XFce 4When XFce 4 is started with the command startxfce4, it will automatically start anything you place in the ~/Desktop/Autostart/ directory. You can place scripts, programs, or symbolic links to scripts and programs in this directory, but you must have permission to execute the scripts and programs for this to work. For example, the following script will find the location of lineakd and start it: #!/bin/bash LINEAKD=`which lineakd` $LINEAKD & You can create the script with your favorite editor, save it as startlineakd, make it executable (chmod +x startlineakd), and place it in the ~/Desktop/Autostart directory. 9.4.2.3 KDEKDE is a good example of a desktop environment that offers more power than you can configure out of the commands available to a startup file such as ~/.xinitrc. Many desktop environments consist of much more than a window manager. They include panels, launchers, and more. When you start a desktop environment, such as KDE, it automatically starts these extra features. All of these features running together are considered a session. You can configure KDE to remember what applications you are running when you log out, and KDE will consider those programs as part of the next session and start them automatically the next time you start KDE. So, one of the simplest ways to start programs automatically in KDE is to save your current session when you exit KDE. In fact, most distributions preconfigure KDE to behave this way automatically, so most of you won't have to turn on this feature. Those of you who don't want KDE to restart the programs you have running when you log out will have to turn off this feature. Some Linux distributions customize the KDE Control Center, but the
typical KDE control panel lets you control session management this
way. Start up the Control Center from the main menu, and select KDE
Components Alternatively, you can start up programs or open files simply by placing them in the ~/.kde/Autostart directory. There are actually two Autostart directories. One is used by KDE to start up applications for every user. The other, the ~/.kde/Autostart directory, is for personal use. If you want to start programs, you can place a symbolic link to the program in this directory. You can do this for scripts as well, or even place the script itself in this directory. In all cases, however, you must have permission to execute any scripts or links you place in this directory. You can also place KDE application launcher files in the
~/.kde/Autostart directory. You can create a
launcher file by right-clicking the desktop, and then selecting
Create New The KDE ~/.kde/Autostart directory also boasts another significant capability that is worth special notice. You don't have to limit what you place in this directory to scripts, programs, or .desktop files. You can also place normal files, such as OpenOffice.org documents, pictures, spreadsheets, or whatever you want in the ~/.kde/Autostart directory. KDE will automatically open these documents as though you clicked their icons in a folder. KDE opens these files by performing the default action for that file type, the same way it has a default action for launching a file when you click it. 9.4.2.4 GNOMEAs with KDE, one simple way to make sure certain programs start automatically when you start GNOME is to set GNOME to save the current session when you exit GNOME. GNOME automatically restores the session the next time you log in to GNOME. If you want to customize which applications you want started automatically, you need to do it through the sessions manager. The menu item for this program appears in the GNOME menu in various places, depending on the Linux distribution, but the menu selection itself is usually called Sessions. Click the third tab of the Sessions dialog and enter a command you want to execute automatically every session (Figure 9-1). Figure 9-1. The Gnome Sessions settings dialog![]() |