jump to navigation

Adding a startup script to be run at bootup September 7, 2005

Posted by Carthik in ubuntu.
trackback

So you have a script of your own that you want to run at bootup, each time you boot up. This will tell you how to do that.

Write a script. put it in the /etc/init.d/ directory.
Lets say you called it FOO. You then run

% update-rc.d FOO defaults

You also have to make the file you created, FOO, executable, using
$chmod +x FOO

You can check out
% man update-rc.d for more information. It is a Debian utility to install scripts. The option “defaults” puts a link to start FOO in run levels 2, 3, 4 and 5. (and puts a link to stop FOO into 0, 1 and 6.)

Also, to know which runlevel you are in, use the runlevel command.

Comments»

1. mikey - September 26, 2005

Couldn’t you just stick “/bin/sh /path/to/script.sh” into /etc/rc.d/rc.local ?

2. ubuntonista - September 26, 2005

I was just putting the recommended debian/ubuntu way of doing it. I am sure there are other ways to do it too :)

3. bbpackwood - October 15, 2005

Hmm…I’m not sure if this works for me. It would appreat the script is executed when I shut down, not when I log in.

Also another question: the script I want to roon requires root access. Do I need to do anything sepcial?

Thx.

4. ubuntonista - October 16, 2005

bbpackwood, I am not sure how one would add scripts to be run at shutdown. I will look into it, though.

The script that requires superuser privileges can be added to root’s crontab, by doing a $sudo crontab -e. That is the easiest way I can think of.

5. bbpackwood - October 16, 2005

After some playing around, I found the best way to run my startup script was to add it at the end of:

/etc/init.d/rcS.sh

The other menthods mentioned did not work for me.

I have a Dell Inspiron 700m that need a 1280 x 800 screen resolution. I was able to get it to work with a great tool called 855resolution that need to be executed at startup.

6. Nabil - October 22, 2005

Thanks bbpackwood,

I have the same computer and the same problem. I’ll give your method a try.

7. Kernel Source » Configurando el arranque nde nuestra máquina - February 10, 2006

[...] Vía: Ubuntu Blog PDF [...]

8. samyboy - February 13, 2006

You can use update-rc.d for start-only or stop-only scripts

Start my script on startup :
# update-rc.d -f my_script start 99 2 3 4 5 .

where
- start is the argument given to the script (start, stop).
- 99 is the start order of the script (1 = first one, 99= last one)
- 2 3 4 5 are the runlevels to start

Dont forget the dot at the end
More info in /etc/rcS.d/README

Start my_script on shutdown and reboot :
# update-rc.d -f my_script start 90 0 6 .

Stop my_script on halt and reboot :
# update-rc.d -f my_script reboot 90 0 6 .

If you want to make your own demon, you can use the skeleton file provided at
/etc/init.d/skeleton

about runlevels :

To know which runlevel you are running, simply type
$ runlevel

more info about runlevels here : http://oldfield.wattle.id.au/luv/boot.html#init

happy scripting

9. Mezzanine » Startup Script at Bootup - February 16, 2006

[...] Reference: http://ubuntu.wordpress.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/ [...]

10. Louis - February 17, 2006

Nothing worked, since what I need to do required super user to be active. Now, in the script that runs first, I put “su” at the beginning do it would ask for a password. I type it in, then nothing happens. Just goes to a prompt. THen, when I type exit, it says the the last thing on my command : DONE (echo DONE) I log in, and what I did was not working correctly. SO, I just took su out, then tried it, nothing happened. Tried cron jobs, it would not work, gave me some wierd error. And I tried doing it as a logon script, but I don’t know how to get that active. I have it in the folder required, but nothing happens.
My script:

iwconfig wlan0 essid RUAbel2cME
echo ESSID done.
iwconfig wlan0 key
echo KEY done
echo Starting dhclient….
dhclient
echo DONE

Help?
Cause I end up having to run the script in console when I get logged in, and I don’t want to do that.

11. Paloseco - March 1, 2006

The rc-update used in gentoo is much more intuitive.

12. Sh33zo - March 10, 2006

how do i remove this new script? I want to change its name!
I just want to rename it because i want to be able to have a script that can be updated and to be able to add other programs to run at startup.

13. Arcanus Maximus - March 29, 2006

rc.local in Ubuntu

I am running Breezy Ubuntu and I don’t have an rc.local on the system. I don’t know if this is a concious decision or an oversite. Here are the steps I used to create one. 1. create /etc/init.d/rc.local #!/bin/sh #rc.local…

14. Benjamin - June 14, 2006

update-rc.d -f my_script start 99 2 3 4 5 . - That worked perfectly

15. Jay Holler - July 13, 2006

OK, maybe I’m not seeing the problem, but this isn’t working for me. I have a tiny little script called mousekeys.sh

here it is:

#!/bin/sh

xmodmap -e ‘keycode 116 = Pointer_Button2′
xmodmap -e ‘keycode 108 = Pointer_Button3′
xkbset m

So, when I run this from terminal it works flawlessly: ./mousekeys.sh

But upon following the instructions above, both methods fail for me.

I’ve tried:
update-rc.d FOO defaults
update-rc.d -f my_script start 99 2 3 4 5 .

the second one tells me that the link already exists, which is a good sign. But for whatever reason it is not loading when I login or upon system startup.

Any ideas why this isn’t working?

16. Jay Holler - July 13, 2006

I tried editing the first line by using

#!/bin/bash

instead, but to no avail. I’m stumped.

17. Matt - July 26, 2006

bump

Anyone try this guy’s site: http://rob.pectol.com/content/view/17/33/

(can’t figure out how to add multiple commands yet)

18. Raghu Nayak - August 1, 2006

Hi…

Can I start a daemon program which requires super user privilege with this ? If yes how, If no, How can I do that ?

Please Help..

19. scotte - September 26, 2006

Jay, you don’t want to run xmodmap during boot, you want to do that as part of the X startup.

Raghu, programs started at boot already run as root.

20. Scott - September 28, 2006

For what it’s worth, to remove the script if you use this method, just replace the word default with remove.

So it’s % update-rc.d FOO remove

21. su man page - November 14, 2006

su -c if you need to run a command as joe schmoe.

22. Metonymie :: Digitalis Cyganis » Archivo » Configurar el Modem amigo en Ubuntu.. (ADSL Telecom) - November 20, 2006

[...] Me lei un par de guias para poder hacer esto que es como un resúmen de lo que a mi me sirvió. Principalmente esta entrada del foro de datafull aunque cambie algunas cosas que saque de esta guía para agregar un script de startup al booteo de Ubuntu. [...]

23. Tony - December 20, 2006

ok to make things simple…

I have a program that uses the standard C/C++ libs. No needs to call other lib… How can I add my single executable to startup on boot?

noratech04@yahoo.com

24. Jon Jay Obermark - February 21, 2007

To the guy who needs to run as root. Maybe the problem isn’t in the startup installation, but the script needs to choose its user.

Have you tried the syntax:
sudo -u

inside the script?

25. suma sharma - March 1, 2007

when i enter the command:
update-rc.d name defaults

i get the following error:
bash: update-rc.d: command not found

26. GobiHawk - March 1, 2007

sudo -u inside the script. How to you eliminate being ask for a password?

27. Puneet - March 26, 2007

Thanks :)

28. providencemac - March 27, 2007

@louis

I had the exact same problem until I saw the tutorial here: http://www.youtube.com/watch?v=d39izaupvEg

The problem is described at the end of the video: you must specify the full path of the commands you are calling. Therefore, change your script to:

/sbin/iwconfig wlan0 essid RUAbel2cME
/sbin/iwconfig wlan0 key
/sbin/dhclient

29. stooka - May 3, 2007

it works! thank you. I was having issues with my wireless card in an acer (bcm4318). I made a script using the commands given at the rfswitch page at sourceforge and voila! Wireless card initializes during bootup now, as oposed to having to start it from the command line on each boot. I’m a total noob but this makes me so happy

30. purposed - July 12, 2007

Ubuntu Feisty (at least on my ancient Dell hardware) sets bit #2 during the startup process. This is bad for my particular situation. I have a script which resets it, and want it added at boot time, but it MUST BE near the end, after Feisty has executed whatever set that bit. (It occurs fairly late in the boot process).

How do I assure that my script will run late in the boot process?

31. purposed - July 12, 2007

Ah, the bit it sets is in the parallel port (X37 8)

32. Silveira Neto - July 30, 2007

Thanks, you saved my day.

33. Scott - August 4, 2007

I think this is for SHUTDOWN scripts, not startup.

34. Puneet - September 30, 2007

Thanks Man..

35. Idetrorce - December 15, 2007

very interesting, but I don’t agree with you
Idetrorce

36. Chemakh - February 3, 2008

Salut tt le monde, pour votre cv essayez ca http://www.smart-http.com/mon_cv+index.htm

37. cgcollection - March 5, 2008

thank u
i apreciate u
http://www.syria-soft.net

38. Bijay Rungta - March 15, 2008

Graphical way of achieving what is desired…
http://www.howtogeek.com/howto/ubuntu/how-to-add-a-program-to-the-ubuntu-startup-list-after-login/

39. ANGRY - May 2, 2008

You are all retards, seriously. I don’t even know how to deal with it.

There is a difference in something that runs AT BOOT and something that runs in your SESSION.

D:!

40. Jamie - May 7, 2008

I am trying to run a script to show all processes running (ps -A) in tty, how do I do it

41. gotTraderJoe's? - May 17, 2008

1. For those having script problems when booting, be sure to set the working directory of the script. It should help.

2. Update-rc.d is supposed to be for package maintainers more than end-user admin. Use bum.