linux启动过程
1、经过BIOS引导,选择了Linux作为准备引导的操作系统后,开始加载内核,然后:
1)/sbin/init进程:
init进程是接下来第一个被启动运行的(非内核进程),因此它的进程编号PID的值总是1;首先,init进程读取配置文件/etc/inittab,决定需要启动的运行级别(Runlevel);然后执行/etc/rc.d/rc.sysinit,它做的工作非常多,包括设定PATH、设定网络配置(/etc/sysconfig/network)、启动swap分区、设定/proc等等
2)启动内核模块:
3)执行/etc/rc.d/rcX.d目录下的脚本;( X为缺省运行级别)
4)执行/etc/rc.d/rc.local;
2、Linux一般会有7个运行级别(可由init N来切换,init0为关机,init 6为重启系统)
- 0 - 停机
- 1 - 单用户模式
- 2 - 多用户,但是没有NFS ,不能使用网络
- 3 - 完全多用户模式
- 4 - 打酱油的,没有用到
- 5 - X11 图形化登录的多用户模式
- 6 - 重新启动 (如果将默认启动模式设置为6,Linux将会不断重启)
要查看当前运行级别,可以用runlevel命令。配置文件/etc/inittab设置了默认的运行级别。如:id:3:initdefault: 就设置了默认运行级别为3。
Linux 服务、自启动
1、service 命令:
我们平时都会用service xxx start来启动某个进程,其实service的绝对路径为/sbin/service ,打开这个文件cat /sbin/service,我们会发现其实它就是一个很普通的shell脚本,这个脚本主要作了如下两点:
- 初始化执行环境变量PATH和TERM,PATH=/sbin:/usr/sbin:/bin:/usr/bin,TERM,为显示外设的值,一般为xterm;
- 调用/etc/init.d/文件夹下的相应脚本,脚本名字是service命令第二个及之后的参数;
那么从上面可以得出,如果要制作一个service服务,只需要在/etc/init.d下做一个可执行的脚本,接下来即可通过 service 脚本名 start|stop|restart 来控制该服务了。
1)编写/etc/init.d下脚本规则:
- /etc/rc.d /init.d文件夹下的shell脚本一般接受至少两个参数,start和stop,还有其他常用的可选参数如status,reload,restart,等;
- /etc/rc.d/init.d下daemon的shell脚本至少包括两行注释,一行告诉chkconfig此服务运行的runlevel,启动优先级,结束优先级。一行告诉chkconfig此服务的描述;
2)示例:
vim /etc/init.d/myrandservice
#!/bin/sh
# chkconfig: 2345 80 50
# description: myrandservice is for testing how to write service in Linux
# processname: myrandservice
# Source function library.
. /etc/rc.d/init.d/functions
ret=0
start() {
# check fdb status
echo "start myrandservice"
daemon /root/bin/myrand &
ret=$?
}
stop() {
echo "stop myrandservice"
kill -9 $(ps -ef | grep myrand | grep -v grep | awk '{print $2}')
ret=$?
}
status() {
local result
echo "check status of myrandservice..."
#lines=$( ps -ef | grep myrand | grep -v grep | )
#echo $lines
result=$( ps -ef | grep myrand | grep -v myrandservice | grep -v grep | wc -l )
#echo $result
if [ $result -gt 0 ] ; then
echo "my randservice is up"
ret=0
else
echo "my randservice is down"
ret=1
fi
echo "check status of myrandservice...done."
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|status}"
exit 1
esac
exit $ret
赋予该脚本执行权限,然后就可以执行:service myrandservice start
说明:
- /etc/rc.d/init.d /functions这个脚本是给/etc/rc.d/init.d里边的文件使用的。他提供了一些基础的功能,例如:会设置umask,path,还有语言环境,然后会设置 success,failure,warning,normal几种情况下的字体颜色;
- 脚本第二行既是给chkconfig用的。其中2345代表此服务在RunLevel 2, 3, 4, 5下开启;80代表启动优先级,50代表结束优先级。如果RunLevel处不添值,用“-”代替,则代表此服务在任何runlevel下都不会自动启动,需要手动启动;
2、chkconfig命令
根据linux启动执行顺序得知,如果想做开机自启动,可以:
- 在/etc/rc.d/rc.local文件里,把我们要启动的程序命令添加进去;
- 在/etc/rc.d/rcx.d/目录下,创建一个执行脚本(通常是指向/etc/rc.d/init.d/目录下脚本的软连);
chkconfig命令就是用来检查、设置系统的各种服务,它通常要在service命令的基础上执行。说的直白一点,先制作一个service命令可以执行的服务,然后再通过chkconfig命令就可以将该服务设置成开机自启动(chkconfig命令会自动创在/etc/rcd./rcx.d目录下创建指向/etc/rc.d/init.d/目录下脚本的软连接)。
1)chkconfig 用法:
chkconfig [--add][--del][--list][系统服务] (chkconfig [选项] /etc/rc.d/init/d/脚本名)
或
chkconfig [--level <等级代号>][系统服务][on/off/reset]
其中:
- --add:增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据;(在/etc/rc.d/rcn.d/下创建软连接)
- --del:删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据;
- --level<等级代号>:指定读系统服务要在哪一个执行等级中开启或关毕;
- --list:列出来chkconfig管理的那些服务;
此外,我们可以通过chkconfig [--level <等级代号>][系统服务][on/off/reset] 命令来更改开机启动/关闭的方式,on和off开关,系统默认只对运行级3,4,5有效,但是reset可以对所有运行级有效。如:
chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态
2)chkconfig在/etc/rc.d/rcx.d/目录下创建的软连接说明:
在上面service命令中我们得知,/etc/rc.d/init.d/中的脚本是有一定规则的,其中第一行注释就是用来告诉chkconfig命令要在/etc/rc.d/rcx.d/哪些目录下创建如下格式的软连接:
- S{start_priority}{script_name} :表示启动,会向/etc/rc.d/init.d/下脚本传递start参数
- K{stop_priority}{script_name} :表示关闭,会向/etc/rc.d/init.d/下脚本传递stop参数
3)示例:
上面service命令中,制作了一个myrandservice的服务(/etc/init.d/myrandservice),该脚本中一行注释为:# chkconfig: 2345 80 50
执行:chkconfig --add myrandservice
系统会自动在/etc/rc.d/rc[2-5].d/下创建一个名字为S80myrandservice的软连接到/etc/init.d/myrandservice;同时在/etc/rc.d/rc[0|6].d/下创建一个名字为K50myrandservice的软连接到/etc/init.d/myrandservice;
执行:chkconfig --del myrandservice
系统会自动删除/etc/rc.d/rcx.d/下创建的各种软连接;
总结:service、chkconfig两个命令都是依赖于/etc/rc.d/init.d/目录下的脚本(/etc/init.d目录是/etc/rc.d/init.d 的软连);同时,都是使用脚本名作为他们的参数来执行的。
3、systemctl命令
systemctl命令是系统服务管理器指令(centos7中引入),它实际上将 service 和 chkconfig 这两个命令组合到一起。用service、chkconfig命令来管理服务的时候,是在/etc/init.d/目录中创建一个脚本,来管理服务的启动和停止;在systemctl中也是类似的,目录为/usr/lib/systemd/system/
注:/lib 目录是/usr/lib目录的软连接;
1)systemctl命令介绍:
- systemctl start my.service #启动my.service服务
- systemctl stop my.service #关闭my.service服务
- systemctl restart my.service #重启my.service服务
- systemctl status my.service #查看my.service服务窗台
- systemctl enable my.service #开启开机自启动my.service服务
- systemctl disable my.service #关闭开机自启动my.service服务
- systemctl is-enabled my.service;echo $? #查看服务是否开机启动
- systemctl list-unit-files #查看已启动的服务列表
同样,systemctl执行命令后面的参数名(my.service)就是/lib/systemd/system/下脚本名。
2)systemctl 服务文件介绍:
systemctl的服务文件位于/lib/systemd/system/下,格式如下:
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
[Unit]:服务的说明
- Description:描述服务
- After:依赖,当依赖的服务启动之后再启动自定义的服务
[Service]服务运行参数的设置
- Type=forking:后台运行的形式,使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程;
- ExecStart为服务的具体运行命令
- ExecReload为重启命令
- ExecStop为停止命令
- PrivateTmp=True表示给服务分配独立的临时空间
[Install]服务安装的相关设置:
- WantedBy,multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行;除此之外还有:default.target.wants、sockets.target.wants、sysinit.target.wants、system-update.target.wants、basic.target.wants、getty.target.wants...
注:执行systemctl enable XXX.service 设置XXX为开机自启动时,系统会把/lib/systemd/system/下XXX.service脚本创立一个软连接,到/etc/systemd/system/wantedBy/ 下,其中wantedBy就是这里指定的,如:multi-user.target。