Linux runlevel

来源:互联网 发布:淘宝超级店长好用吗 编辑:程序博客网 时间:2024/04/28 22:14

Linux runlevel:

The term runlevel refers to a mode of operation in one of the computer operating systems that implement Unix System V-style initialization. Conventionally, seven runlevels exist, numbered from zero to six; though up to ten, from zero to nine, may be used. S is sometimes used as a synonym for one of the levels. Only one "runlevel" is executed on bootup - run levels are not executed sequentially. Eg: either runlevel 2 OR 3 OR 4 is executed, not 2 then 3 then 4.

"Runlevel" defines the state of the machine after boot. Different runlevels are typically assigned to:

    * single-user mode
    * multi-user mode without network services started
    * multi-user mode with network services started
    * system shutdown
    * system reboot

一般的linux会提供三种runlevel:

0: shutdown or halt the system
1: single-user mode,usually aliased as s or S
6: Reboot the system

此外不同的linux可能会提供不能的runlevel:
http://en.wikipedia.org/wiki/Runlevel

 

查看default runlevel的方法:
rcc-pok-idg-2318:/var/log # cat /etc/inittab  | grep id
id:3:initdefault:
# <id>:<runlevels>:<action>:<process>
# The "id" field  MUST be the same as the last

 

修改runlevel的方法:

1.修改/etc/inittab

2使用telinit

Usage: telinit 0123456SsQqAaBbCcUu

 

telinit其实是init的软链接

 

 

rcc-pok-idg-2318:/sbin # ls -l | grep telinit
lrwxrwxrwx 1 root root        4 Feb 19 19:56 telinit -> init

 

 

rcc-pok-idg-2318:/sbin # shutdown --help
shutdown: invalid option -- '-'
Usage:    shutdown [-akrhHPfnc] [-t secs] time [warning message]
              -a:      use /etc/shutdown.allow
              -k:      don't really shutdown, only warn.
              -r:      reboot after shutdown.
              -h:      halt after shutdown.
              -P:      halt action is to turn off power.
              -H:      halt action is to just halt.
              -f:      do a 'fast' reboot (skip fsck).
              -F:      Force fsck on reboot.
              -n:      do not go through "init" but go down real fast.
              -c:      cancel a running shutdown.
              -t secs: delay between warning and kill signal.
              ** the "time" argument is mandatory! (try "now") **

 

在linux下一些常用的关机/重启命令。

1.shutdown

shutdown命令安全地将系统关机。
有些用户会使用直接断掉电源的方式来关闭linux,这是十分危险的。因为linux与windows不同,其后台运行着许多进程,所以强制关机可能会导致进程的数据丢失﹐使系统处于不稳定的状态﹐甚至在有的系统中会损坏硬件设备。
而在系统关机前使用shutdown命令﹐系统管理员会通知所有登录的用户系统将要关闭。并且login指令会被冻结﹐即新的用户不能再登录。直接关机或者延迟一定的时间才关机都是可能的﹐还可能重
启。这是由所有进程〔process〕都会收到系统所送达的信号〔signal〕决定的。这让像vi之类的程序有时间储存目前正在编辑的文档﹐而像处理邮件〔mail〕和新闻〔news〕的程序则可以正常地离开等等。
shutdown执行它的工作是送信号〔signal〕给init程序﹐要求它改变runlevel。Runlevel 0被用来停机〔halt〕﹐runlevel 6是用来重新激活〔reboot〕系统﹐而runlevel 1则是被用来让系统进入管理工作可以进行的状态﹔这是预设的﹐假定没有-h也没有-r参数给shutdown。要想了解在停机〔halt〕或者重新开机〔reboot〕过程中做了哪些动作﹐你可以在这个文件/etc/inittab里看到这些runlevels相关的资料。

shutdown 参数说明:

[-t] 在改变到其它runlevel之前﹐告诉init多久以后关机。
[-r] 重启计算器。
[-k] 并不真正关机﹐只是送警告信号给每位登录者〔login〕。
[-h] 关机后关闭电源〔halt〕。
[-n] 不用init﹐而是自己来关机。不鼓励使用这个选项﹐而且该选项所产生的后果往往不总是你所预期得到的。
[-c] cancel current process取消目前正在执行的关机程序。所以这个选项当然没有时间参数﹐但是可以输入一个用来解释的讯息﹐而这信息将会送到每位使用者。
[-f] 在重启计算器〔reboot〕时忽略fsck。
[-F] 在重启计算器〔reboot〕时强迫fsck。
[-time] 设定关机〔shutdown〕前的时间。

2.halt—-最简单的关机命令

其实halt就是调用shutdown -h。halt执行时﹐杀死应用进程﹐执行sync系统调用﹐文件系统写操作完成后就会停止内核。

参数说明:

[-n] 防止sync系统调用﹐它用在用fsck修补根分区之后﹐以阻止内核用老版本的超级块〔superblock〕覆盖修补过的超级块。

[-w] 并不是真正的重启或关机﹐只是写wtmp〔/var/log/wtmp〕纪录。
[-d] 不写wtmp纪录〔已包含在选项[-n]中〕。
[-f] 没有调用shutdown而强制关机或重启。
[-i] 关机〔或重启〕前﹐关掉所有的网络接口。
[-p] 该选项为缺省选项。就是关机时调用poweroff。

3.reboot

reboot的工作过程差不多跟halt一样﹐不过它是引发主机重启﹐而halt是关机。它的参数与halt相差不多。

4.init

init是所有进程的祖先﹐它的进程号始终为1﹐所以发送TERM信号给init会终止所有的用户进程﹑守护进程等。shutdown就是使用这种机制。init定义了8个运行级别(runlevel),init 0为关机﹐init1为重启。关于init可以长篇大论﹐这里就不再叙述。另外还有telinit命令可以改变init的运行级别﹐比如﹐telinit -iS可使系统进入单用户模式﹐并且得不到使用shutdown时的信息和等待时间。

5.poweroff

关机,在多用户方式下(Runlevel 3)不建议使用.

 

 

 

set 用于设置环境变量
unset 用于取消环境变量
setenv:有set和export的功能

 

在登录Linux时要执行文件的过程如下:
在刚登录Linux时,首先启动 /etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一个,执行的顺序为:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的话,一般还会执行 ~/.bashrc文件。因为在 ~/.bash_profile文件中一般会有下面的代码:

if [ -f ~/.bashrc ] ; then
 . ./bashrc
           fi
  ~/.bashrc中,一般还会有以下代码:
if [ -f /etc/bashrc ] ; then
 . /bashrc
fi

所以,~/.bashrc会调用 /etc/bashrc文件。最后,在退出shell时,还会执行 ~/.bash_logout文件。

执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc ->          /etc/bashrc -> ~/.bash_logout

关于各个文件的作用域,在网上找到了以下说明:
(1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。

(2)/etc/bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。

(3)~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。

(4)~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。

(5)~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件. 另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系。

(6)~/.bash_profile 是交互式、login 方式进入 bash 运行的~/.bashrc 是交互式 non-login 方式进入 bash 运行的通常二者设置大致相同,所以通常前者会调用后者。

 

原创粉丝点击