linux startup scripts
------
init & run level
init process define 7 run level:
* level 0
shutdown
* level 1 or S
single user mode,
* level 3-5
multiple user mode,
2 or 3, is command line mode,
5, is x-window mode,
4, rarely used,
* level 6
reboot
config file:
location:
/etc/inittab
config init process, like default run level,
e.g.
id:2:initdefault:
------
scripts
location:
/etc/init.d/
orginal startup shell scripts, each script is for a service or an aspect of service,
accept args:
start, stop, restart(not sure support),
/etc/rcx.d/
the x could be: 0,1,2,3,4,5,6,S,
inside each folder, are symbol links to scripts in /etc/init.d,
execute rules:
for each level, init process will execute scripts in relative "/etc/rcx.d/" folder,
args for scripts:
start, if script name start with S,
stop, if script name stop with K,
order:
order depends on xx,
customize scripts:
steps:
* add/modify/remove orginal scripts in /etc/init.d/,
* add symbol links to /etc/rcx.d/ folder, start with S|K, and a proper order number,
distribution specific scripts style:
different distributions, might have some specific scripts style,
e.g. debian/ubuntu has a script "/usr/sbin/update-rc.d" to help customize startup scripts,
e.g.
under /etc/rc3.d/
// start oracle-xe in order 20, in level 3
ln -s S20oracle-xe ../init.d/oracle-xe
under /etc/rc0.d/
// stop oracle-xe in order 20, in level 0
ln -s K20oracle-xe ../init.d/oracle-xe
------
debian & ubuntu run level
run level:
0 shutdown
1 single
2 multiple user, default,
3-5 same as 2,
6 reboot
/etc/inittab file:
debian&ubuntu don't have this file by default, you need to create this file, then use it,
update-rc.d command:
help to add/remove init scripts,
use "man update-rc.d" to check help,
e.g.
// add S20foo for level 2/3/4/5, and add K20foo for level 0/1/6, links for foo should not already exists,
update-rc.d foo defaults
// remove all links for foo,
update-rc.d -f foo remove
// add S30foo for level 2/3/4/5, and add K25foo for level 0/1/6, links for foo should not already exists,
update-rc.d foo start 30 2 3 4 5 . stop 25 0 1 6 .
------
e.g.
* add a script to echo a line to a file when startup & shutdown
* script, foo:
#! /bin/sh case "$1" in start) echo -e "startup, $(date)\n" >> /home/eric/startup_script_test.txt ;; stop) echo -e "shutdown, $(date)\n" >> /home/eric/startup_script_test2.txt ;; *) echo "Usage: param only support start|stop" exit 1 ;; esac
* add script to run level - by hand:
startup (level 2):
su
cd /etc/rc2.d
ln -s S99foo ../init.d/foo
shutdown:
su
cd /etc/rc0.d
ln -s K99foo ../init.d/foo
restart:
when restart scripts in startup will be execute, but scripts in shutdown might not,
* remove script from run level - by hand:
just remove symbol links,
* add script to run level - ubuntu update-rc.d:
update-rc.d foo defaults
* remove script from run level - ubuntu update-rc.d:
update-rc.d -f foo remove
* change ubuntu default run level
* create /etc/inittab, if not exists,
su
vi /etc/inittab
save it,
* set default run level
vi /etc/inittab
add/modify line:
id:3:initdefault:
save it,
*
------