Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I have a particular python script always run on my system automatically when my system starts until I shut down?

There should be some simple way by which I can just put that small script in some init script or something, which keeps running without me invoking it.

I am using Ubuntu 10.04, if that makes a difference.

share|improve this question

migrated from Dec 5 '10 at 11:09

This question came from our site for professional and enthusiast programmers.

at what stage of system startup you want this script to run? before user login or after? – max taldykin Dec 5 '10 at 11:00
after user login – zubinmehta Dec 5 '10 at 11:01
up vote 6 down vote accepted

put the script into /etc/init.d/ ?

share|improve this answer

You can (under Gnome) go to System>>Preferences>>Sessions and add your script here.

share|improve this answer

You mention needing it to run after a user logs in, but that it runs until the system is shut down. That's a bit conflicting, because usually things that would run in an X session would stop when the session is shut down. Additionally, if you want it to run on a non-X-windows session, you'll have to be very careful about how it reacts with running multiple times or be careful to start it only once.

That said, here are some ways to run scripts:

  • In a user crontab use "@reboot" for the time specification, like: @reboot /path/to/script This will cause it to run as the specified user, and will start at system boot time and run until either it dies or the system is shut down.
  • Go into the "Settings" application (XFCE: Applications -> Settings -> Xfce 4 Settings, GNOME: System -> Preferences, etc) and modify the applications that are started for your session. You can add a script to run as part of the session. This will start and run while your session is running, but it won't be restarted if the script dies.
  • The session script could be wrapped by a script that runs the Python program in the background, so that it would continue running after session logout, but then you have to make sure that if a user logs out and back in that they don't start multiple copies.
  • If you want it to run from shell sessions via SSH or the like, rather than an X windows session, you could put it in the .profile (.bash_profile, .zprofile, etc). But you're going to need to background it as above and be sure multiple copies aren't running.
  • You could just put it in /etc/rc.local and either run it as the user (via "su"), but that's going to start at boot time, not login time. Personally, I'd recommend cront+@reboot over this.
  • You could create an upstart script in "/etc/init", this will cause it to start at boot time as above, but you can make it restart the script if it dies for any reason. This is the best option if you need to make sure it continues running.
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .