In Ubuntu, telling an application to start at login is as easy as it gets. Just use the GUI. Go to System → Preferences → Startup Applications. Then click Add and type in the command for the application you want to start.
If you don’t know the right command, you can find it from that application’s launcher. Just go to System → Preferences → Main Menu, highlight the application you’re trying to start, and click Properties. You can copy the command to run the application (along with the name and description) from the Properties Dialog that will appear. Easy, right?
Ok, but what if the program you’re trying to run at startup requires root access? Normally, you would start it with sudo from the terminal or with gksu from the run dialog (the dialog that pops up with you press Alt+F2). But we’re trying to get this application to start itself when you log in. Some people might tell you to try to start this program with Ubuntu’s Upstart init daemon or with an /etc/init.d script. However, that would make the application run as a service, and it is not appropriate for all applications. Any program that requires GNOME or X (all graphical programs and some others) will fail at boot if they start before GNOME finishes its startup sequence. If your program doesn’t have a lot of dependencies, and does not have a GUI, then you might consider that approach (see my post: Run Your Program as a Service with Upstart in Ubuntu). Otherwise, here is an alternative approach:
$ sudo visudo
This command will use your default console text editor to open a file called /etc/sudoers.tmp. Do not try to edit that file without entering the visudo command. As a side note, I read somewhere that you can change your default console text editor with something like: export VISUAL=”pico -w”. I’ve never tried it, I use gedit for everything. Anyway, this is what you should see (it might look a little different in a different editor, but the text will be the same):
What you want to do is allow a certain user (yourself) to use sudo to gain root access to a certain resource (your startup application) without prompting for a password. Let’s say your username is username and your application is at /usr/local/startup_application.py. In that case, you’ll want to add the following line to the bottom of the file and then save it:
username ALL=NOPASSWD: /usr/local/startup_application.py
Now try running your application from the terminal using sudo. It should start without prompting for a password! Just add your application to the list of Startup Applications using the GUI tool as described above and make sure to use sudo in the command. It should start automatically next boot.
One down side to this method is you don’t have an easy way to kill the process if you want to stop it, as you would when using Upstart. If the program has a GUI, that makes it easy. However, you can still kill the app if it runs in the background. Just do this (assuming your app is at /usr/local/startup_application.py):
$ ps -C "/usr/local/startup_application.py"
PID TTY TIME CMD
2289 ? 00:00:57 python2.6
You can see that the process ID for your app is 2289. Just kill it with:
$ sudo kill 2289
Now if I had only figured all this out a year ago… Oh well.
Hello,
Great help to me too!
Just one additional question :
I try to use this method for starting an application that interfaces with an ISDN card. This card has it’s own startup which I found as S19xxxxx script in/etc/rc2.d, and my application needs to wait for this, or at least delay some seconds before starting. It seems to me that upstart scripts in /ect/init (like my application) is started before this one, and so it fails to make proper connection.
Is there a way to make upstart scripts depending on rc2.d scripts – or (second choice) delay upstart scripts (e.g.with a timer ?)
tx,
Herman
Herman,
Sorry, I don’t know how to help you with that. Try StackOverflow, that’s where I got help with upstart in the past.
I know this was from some time ago, but this was a huge help and very easy to understand… for a windows guy like me in a Ubuntu world. Thank you for the post!