tags:

views:

109

answers:

2

How do I programmatically set an executable on Linux to run when the user logs in?

Basically, the equivalent of the HKCU\Software\Microsoft\Windows\CurrentVersion\Run registry key in Windows.

+4  A: 

For gnome on Linux, place a .desktop file referring to your application in ~/config/autostart/, the format is fairly simple:

[Desktop Entry]
Type=Application
Exec=foo
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=foo

and you will have to set the exec bit for this file (chmod +x)

If you are shipping on Linux, you should create one of these anyway and place it in the menus.

For KDE on Linux you should create a symbolic link to your program's executable in the folder ~/.kde/Autostart/

ternaryOperator
Thank you! What is the `X-GNOME-Autostart-enabled` key for? Would I need something similar if I want this to work on KDE as well? Also, I changed this question to just reflect Linux since if someone else answers for Mac OS X I can't accept an answer for each.
Jake Petroules
The X-GNOME-Autostart-enabled key tell it to actually start the program, if it were set to false then it would appear disabled in the list of startup programs. I have updated the entry to also include details for KDE. The .bashrc solution is really only recommended for command line programs.
ternaryOperator
+1  A: 

If you don't have GNOME, you'd typically put your commands in a shell's .profile or .rc, or in startx if you want this to run in/before your X-environment. You could also hook something into your gdm scripts/.rcs or whatever login manager you have.

sheepsimulator
What are the benefits of using .desktop files over this approach and vice versa? Would it be best to use .desktop files and use this as a fallback?
Jake Petroules
@Jake Petroules - If everyone is using GNOME and the above recipe works in one spot for all users, it works and I'd roll with the above answer. If not everyone is using GNOME, then it (probably) won't work. The key here is to put the commands in the startup_script _everyone_ who logs in will use. You'll have to figure out what that is, and how to have it work universally. Ideally, you should only have to modify a single file to get this to work.
sheepsimulator
@Jake Petroules - I'll admit - my answer isn't very pointed as to exactly where. I do know the above places do execute when people log in, and they're more universal than GNOME. Someone with better knowledge than I will have to chime in. But that should get you started on a (more universal) path, methinks.
sheepsimulator
On the other hand, generally desktop applications should use the target desktop manager's technique for launching applications. With the other approach you may find problems with the stage at which your program is ran (i.e. do you really want it starting before the desktop environment) and users will be unable to remove it is they so please.
ternaryOperator
@ternaryOperator - That may be true. Like all things in *nix though, there's more than one way to solve it. OP wasn't terribly detailed about the commands he needs to run. It's useful to have as many options laid out as you can, even if yours is best for a given situation. Cheers!
sheepsimulator
Sorry, I should have specified that it's a GUI application based on Qt. I think the desktop files will be the best choice due to what you both said and the popularity of GNOME and KDE... at least for now. Thank you both for your input.
Jake Petroules
@sheepsimulator Of course - both are good depending on the context.
ternaryOperator