Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to execute a command when Ubuntu boots. The commands in particular are:

sudo shutdown +m
sudo shutdown +1

I want the computer to shut down as soon as it starts for testing purposes. I plan on switching the computer back on with a bit of circuitry I am testing.

How can I execute this command automatically at system start up?

share|improve this question
3  
"auto shutdown at boot" sounds evil. :) –  Lamb Sep 7 '12 at 13:00
add comment

3 Answers 3

up vote 6 down vote accepted

Place it in /etc/rc.local. (It is run as root, so sudo is not needed there.)

Also, you may be interested to read additional info about runlevels: http://en.wikipedia.org/wiki/Runlevel

share|improve this answer
    
You can also go to System/Preferences/Startup Applications, and add the script there. –  SaintWacko Sep 7 '12 at 13:06
add comment

There are different ways to automatically run commands:

  1. The upstart system will execute all scripts form which it finds a configuration in directory /etc/init. These scripts will run during system startup (or in response to certain events, e.g., a shutdown request) and so are the place to run commands that do not interact with the user; all servers are started using this mechanism. You can find a readable introduction to at: http://upstart.ubuntu.com/getting-started.html the man pages man 5 init and man 8 init give you the full details.

  2. A shell script named .gnomerc in your home directory is automatically sourced each time you log in to a GNOME session. You can put arbitrary commands in there; environment variables that you set in this script will be seen by any program that you run in your session. Note that the session does not start until the .gnomerc script is finished; therefore, if you want to autostart some long-running program, you need to append & to the program invocation, in order to detach it from the running shell.

  3. The menu option System -> Preferences -> Startup Applications allows you to define what applications should be started when your graphical session starts (Ubuntu predefines quite some), and add or remove them to your taste. This has almost the same purpose and scope of the .gnomerc script, except you don't need to know sh syntax (but neither can you use any sh programming construct).

share|improve this answer
add comment

For simple things you can add a command in System->Preferences->Sessions pointing to the location of your script.

Alternatively you can add it to /etc/init.d/rc.local or make an upstart job if it's a more low level stuff.

Take a look at https://help.ubuntu.com/community/UbuntuBootupHowto for more info

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.