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
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 (X378)

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. Chemakh - February 3, 2008

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

36. Bijay Rungta - March 15, 2008
37. 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:!

38. 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

39. 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.

40. John - June 9, 2008

I need to run a reboot script (which i’ve already written) in Fedora 9 to run at bootup before xwindows is started preferably. I’m using it to test my RAID cards functionality and I need to make sure the driver is loaded before running the script or it will lock the boot process. (for some reason it wont time out and continue on)

41. alecm3 - August 1, 2008

I am new to ubutu, but not new to Linux at all.

Ubuntu needs a better startup scripts manager, like chkconfig in Red Hat/Fedora and SuSE.

update-rc.d does not allow even to list the status of all startup scripts for each runlevel.
No wonder this thread has 43 comments…

42. Jon Ramvi - August 19, 2008
43. Roo’s View » Blog Archive » Installing and Using SlimRIO - September 3, 2008

[...] the SSDP server automatically started at boot time is trivial.  I originally found an article on how to do it in Ubuntu, but it was also in the Debian policy manual had I read it more [...]

44. Riccardo - September 20, 2008

Thanks! I had tried many times to do this and finally found out what I was missing: update-rc.d!
Problem solved, thanks!

45. weltfleeple - October 6, 2008

красивая аэрография автомобилей в Москве

46. Alex Florentino » Blog Archive » Adicionando um serviço para ser inicializado no ubuntu - October 17, 2008
47. Gambas - Can’t connect to local MySQL server through socket workaround | Arejae.Com - October 21, 2008

[...] Start Script at bootup Close this Window Bookmark and Share This Page Save to Browser Favorites / BookmarksAskbackflipblinklistBlogBookmarkBloglinesBlogMarksBlogsvineBuddyMarksBUMPzee!CiteULikeco.mmentsConnoteadel.icio.usDiggdiigoDotNetKicksDropJackdzoneFacebookFarkFavesFeed Me LinksFriendsitefolkd.comFurlGoogleHuggJeqqKaboodlekirtsylinkaGoGoLinksMarkerMa.gnoliaMister WongMixxMySpaceMyWebNetvouzNewsvineoneviewOnlyWirePlugIMPropellerRedditRojoSegnaloShoutwireSimpySlashdotSphereSphinnSpurlSquidooStumbleUponTechnoratiThisNextWebrideWindows LiveYahoo!Email This to a FriendCopy HTML:  If you like this then please subscribe to the RSS Feed.Powered by Bookmarkify™ More » If you’re new here, you may want to subscribe to my RSS feed or get my latest post directly in your mailbox. Thanks for visiting ! Related Post:How to program Gambas with MySQL in UbuntuHow to Setup Your Web Development Server in 7 MinutesHow to run Apache and MySql as a servicesAutoIT – Connecting to SQL Server.MySQL bought by Sun Microsystems Can’t find what you are looking for? Go Gooogle… [...]

48. things to look at (September 20th - October 26th) | stimulant - changing things around. . . - October 26, 2008

[...] Adding a startup script to be run at bootup « Ubuntu Blog [...]

49. Dev kumar - November 6, 2008

I had create 30 script,its runing very well.after one day its not runing.The Run started: 11/6/2008 – 11:38:21 Run ended: 11/6/2008 – 11:38:34 diffrence is very high.But when script is Runing Run starting & ended time diffrence 1or 2.any can give the solution for that problem.

Thanks & Regards
Dev

50. hyperdanja » Blog Archive » links for 2008-11-15 - November 15, 2008

[...] Adding a startup script to be run at bootup « Ubuntu Blog (tags: ubuntu shell services startup) [...]

51. لینک ها 2008-11-17 | Pc-aras - November 17, 2008

[...] Adding a startup script to be run at bootup « Ubuntu Blog [...]

52. Dennis Quek - December 3, 2008

If I put my scripts into init.d, how do i know when it will start, at rc1 2 3 4 or 5 ? or i i have to explicitly add a link inside those rc folders ?

I will need my script to be run after apache starts hmmmm…. ill see when my server reboots !

53. rajan - December 10, 2008

nice and clean. thanks!

54. Zlatko - December 11, 2008

@Dennis> update-rc acriptname default will add links for you on all runlevels.

55. nurettin - December 15, 2008

thanks very good ;)

56. NDLBox - December 23, 2008

Great tip, thanks!

57. frenchn00b - December 28, 2008

and if it is an users , how do you do?

58. Chicken Doughnut » Blog Archive » Installing Tomcat as a Linux Service - January 8, 2009

[...] Adding a startup script to be run at bootup [...]

59. Synaptic Misfirings » Share folders between Virtualbox Windows host and Ubuntu guest - January 9, 2009

[...] Adding a startup script to run at bootup [Ubuntu Blog] [...]

60. santhosh - January 29, 2009

in my boot script i download a file and exwcute the downloaded file.

My bootscript is getting executed twice ??

I executed “update-rc.d myscript defaults”
any reason why its getting executed twice and how do I make it run only once?

61. kienan - January 30, 2009

doods. someone should just make a script that runs at startup that basically launches all of the scripts/programs in a certain directory that can be added to by the user via drag and drop. and then just have another script that runs all the programs/scripts in another folder at logoff/shutdown. i know that puppylinux does this right off the disk…. not sure what script it is tho… this would make it a lot easier to run things @ boot/shutdown.

62. Vik - February 7, 2009

hey everyone!
I am facing a small problem. I have written some (10-12) python scripts which I want to run at boot up. I know that they need to be placed in /etc/init.d and should be made executable with the command chmod +x .

But then I want a service to be started at the bootup called as “scripts” and in turn will start all the scripts which I have written.
It shud show something like this at the boot up and in turn will start all the scripts.

Starting scripts: [ OK ]

Any help is appreciated. Thanks in advance

Best Regards
Vik

63. TheOther1 - February 16, 2009

Vik,

Try this:

Create script in /etc/init.d named services

#!/bin/sh
# /etc/rc.d/sripts
# Description: Starts Python scripts
# ————————————————–
#
### BEGIN INIT INFO
# Provides: Scripts
# Required-Start: $network
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start Python scripts to provide X,Y and Z
### END INIT INFO

case “$1″ in
start)
echo -n “Starting Python Scrits: ”
/usr/local/bin/script1.py
/usr/local/bin/script2.py
;;
stop)
echo -n “Stoping Python Scrits: ”
/usr/local/bin/script4.py
/usr/local/bin/script5.py
;;
restart)
echo -n “Retarting Python Scrits: ”
/usr/local/bin/script4.py
/usr/local/bin/script5.py
/usr/local/bin/script1.py
/usr/local/bin/script2.py
;;
*)
echo “Usage: scripts {start|stop|restart}”
exit 1
esac

Make /etc/init.d/scripts executable. Make symlinks in the runlevels you want it to run:
eg: for start in runlevel 3:
ln -s /etc/init.d/scripts /etc/init.d/rc3.d/S10scripts
Note that 10 is just an example, you need to decide in what order the script is called.

If running SLES, you can do:
insserv -v scripts
and it will make all of the symlinks for you and start it in the proper sequence based on the Required-Start: line. I’m sure there is a RedHat/Debian/Ubuntu/Gentoo utility that will do the same.

HTH!!!

64. TheOther1 - February 16, 2009

I meant to say make a file named /etc/init.d/scripts in above post.

65. TheOther1 - February 17, 2009
66. Hell - February 20, 2009

i used command $update-rc.d myscript.sh defaults and now i can’t boot my ubuntu 8.10. Can someone help?

By the way, what do you mean saying: “You then run % update-rc.d FOO defaults”. What does that % should mean?

67. spoon - February 21, 2009

% just means the prompt. What does you myscript.sh default do?

68. Kiran G V - February 25, 2009

#! /bin/sh
# /etc/init.d/blah
#

# Some things that run always
touch /var/lock/blah

# Carry out specific functions when asked to by the system
case “$1″ in
start)
HOST=’10.147.251.90′
USER=’tritech’
PASSWD=’tritech’
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
get candle1.C
END_SCRIPT
;;
stop)
echo “Starting script blah ”
;;
*)
exit 1
;;
esac

exit 0

this is my script in which i am trying to copy the file candle1.C from an ftp server at the time of starting the system so i had put this file in /etc/init.d/blah .but it is not working , instead of these ftp commands if am using mkdir d something like command it is working means this script is working but for using an ftp what i have to do , or it is possible to start an ftp connection at the time of init process.plz help me.

69. Vik - February 26, 2009

The other1.. thanks!
There has been a change in the stuff I was doing, so cudnt get back.

I need to start those python scripts as service as well as a daemon listening to some port. That daemon has to do nothing but keep listening and can be killed whenever we wud want.
I have configured a simple daemon and it looks like its listening now.
I’m pretty much on the end game now.
Thanks dude..

70. Tina - March 4, 2009

Hi all

I face one problem in Redhat linux. I added some route manually by
” route add host netmask dev eth” . But my added routes are sink at all after reboot. I want to make these added routing perminantely. I will be appreciated for you all help. Share me the way how can i edit to start up command to make run whenever reboot.

Bgds
Tina

71. links for 2009-03-04 « My place - March 5, 2009

[...] Adding a startup script to be run at bootup « Ubuntu Blog chmod +X FOO; update-rc.d FOO defaults (tags: ubuntu bash shell startup script add) [...]

72. Jonathon Hill » Auto-start a shell script on Ubuntu Server - April 23, 2009

[...] Adding a startup script to be run at bootup [...]

73. sree - June 2, 2009

will the same syntax apply for c++ scripts cause when i tried to add it to startup nothing happened.(it dint run)

74. James Little - June 17, 2009

Thanks for the post. I wanted my script to run last (i.e. 99th) to be sure that networking was up (and the script needed both start and stop operations), so I ran the following:
update-rc.d -f my_script_name defaults 99

Works nicely on Ubuntu 8.04.

75. Konteyner - September 1, 2009

There has been a change in the stuff I was doing, so cudnt get back. thank you

76. Godfrey - September 3, 2009

my dell laptop can not boot up, hangs with a blank screen and a cosior could move around.
Help
Godfrey

77. Tithen - November 11, 2009

Hey I’m new too linux and i have almost no knowledge of code.
I was wondering i could make a script to download and execute this: http://ubuntu.online02.com/node/14

at start up on my live usb? or is there another way of doing it?
I have Ubuntu 9.04 and I’m using at school :P

78. Brian - November 20, 2009

I’m having this odd issue.. I’m trying to get tomcat 5.5.26 to autostart on an Ubuntu 8.04 Hardy Heron VM built using VMWare Studio 1.0.

For whatever reason.. Tomcat will only stay started if the start script symbolic link resides in /etc/rcS.d/ .. If I place the start-up script into /etc/rc2.d/ Tomcat will start, but it shuts down once I get to the VAMI (Virtual Appliance Machine Interface) console. The Tomcat logs indicate it received a shutdown command which it never should have.

79. d3\/!150m4+!c - November 28, 2009

If you’re using the recent version of ubuntu then adding a command line to startup is extremely easy. Open your startup applications via system –> preferences. From there you “click” add then put your command in the “command line”. There shouldn’t be a need to select a program. ;)

80. JL - December 8, 2009

Running Ubuntu Karmic. I’m trying to mount a USB key on boot, and keep it mounted during the login process. The key has my ecryptfs passphrase. I have no problems mounting it on boot, but gnome-volume-manager seems to want to unmount it and then remount it so that I get an icon on the desktop. Problem is, with the key unmounted, I can’t properly mount the encrypted home directory. Any way around this? I’m been messing with halevt, without success so far. Thanks.

81. Ruud Mantingh - March 25, 2010

@ 79. d3\/!150m4+!c

That’s only true if you want the script to start at login and not boot time. Some people want to run scripts at boot time without the need to log in.

82. Alquiler de barcos en Ibiza - March 27, 2010

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

83. vipsites - April 1, 2010

Зарубежные фирмы посредники, выпускающие и продающие запчасти оптом грузовики, имеют тесные отношения с другими фирмами. Например, американская компания Freightliner собирает грузовые автомобили из поставляемых комплектующих.

84. bayan escort - May 2, 2010

thanksss admin

85. Azoor - June 10, 2010

I couldn’t start up tomcat server using init scripts.

any way to increase the execution time of init script at Startup.

86. Augusto Carvalho - June 11, 2010

for me that works fine.
Thanks, mate.

I created a script to start mongrel and redmine.

Cheers.

87. Azoor - June 14, 2010

could you please send that script…

88. george varghese k - July 3, 2010
89. escort bayan - July 6, 2010

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?

90. Bonsale - July 28, 2010

Совет по быстрой продаже бизнеса :

Выставляя сразу максимальный ценник на свой бизнес, начинающий продавец
ограничивает число покупателей. Позиция «возможен торг» изначально не
правильная. Если Вы хотя бы раз публично снизите цену, то логика «значит с
бизнесом что-то не то» позволит претендовать покупателю на нижнюю планку.
Цена бизнеса находится где-то между «стоимостью покупателя» и «стоимостью
продавца». Ищите «золотую середину»!

91. Top News - August 30, 2010

thanks that’s nice post