Wednesday, August 19, 2009

How to run a script in startup

I needed this feature to run a "read-only" mount for windows file system on startup (because I always hibernate Windows, so Ubuntu can't mount it "read-write" by default on startup)

1- Write the script in a file and save it in /etc/init.d
sudo gedit /etc/init.d/my_script

2- make the file executable
chmod +x /etc/init.d/my_script

3- add the script to rc.d files
sudo update-rc.d my_script defaults

And .... it's done.

There is a GUI tool to edit the rc.d called Boot-Up Manager (bum) , but I haven't try it. It can be found here

Note: Many thanks to Ahmad Amr for adding me as a contributer in this blog .....

3 comments:

Ahmad Amr said...

cool! Thanks

Mohammed Hewedy said...

Great post Osama, Thank you.

Could you please write what to put in my_script specifically?

Ahmad Amr said...

Small update: The above method by Osama is 100% correct but the script should be standardized according to the Debian manual (this caused my script to run better with much less hanging):

"Packages that include daemons for system services should place scripts in /etc/init.d to start or stop services at boot time or during a change of runlevel. These scripts should be named /etc/init.d/package, and they should accept one argument, saying what to do:

start

start the service,

stop

stop the service,

restart

stop and restart the service if it's already running, otherwise start the service

reload

cause the configuration of the service to be reloaded without actually stopping and restarting the service,

force-reload

cause the configuration to be reloaded if the service supports this, otherwise restart the service.

The start, stop, restart, and force-reload options should be supported by all scripts in /etc/init.d, the reload option is optional"

and here is my script to start my preferred wireless driver at startup (but only contains the start, shutdown and restart options):

#!/bin/bash

function startCompatWireless() {

cd /home/aamr/Desktop/Desktop.Backup/compat-wireless-2009-08-15
make load

}

function stopCompatWireless() {

ifconfig wlan0 down
modprobe -r ath5k

}

case "$1" in
start)
startCompatWireless
;;
stop)
stopCompatWireless
;;
restart)
stopCompatWireless
startCompatWireless
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

exit 0