linux startup scripts

本文详细介绍了Linux系统的启动过程及不同运行级别的定义。涵盖了init进程、运行级别配置文件(/etc/inittab)、启动脚本的位置与执行规则,并提供了定制启动脚本的方法,包括使用update-rc.d命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,

 

 

------


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值