在介紹init進程前咱們先了解下什麼是進程
1.進程的概念windows
所謂進程就是系統中正在運行的程序,進程是操做系統的概念,每當咱們執行一個程序時,對於操做系統來說就是建立了一個進程,在這個過程當中操做系統對進程資源的分配和釋放,能夠認爲進程就是一個程序的一次執行過程。
2.Linux下的三個特殊進程centos
Linux下有三個特殊的進程idle進程(PID=0),init進程(PID=1),和kthreadd(PID=2)
idle進程由系統自動建立,運行在內核態
idle進程其pid=0,其前身是系統建立的第一個進程,也是惟一一個沒有經過fork或者kernel_thread產生的進程。完成加載系統後,演變爲進程調度、交換。
kthreadd進程由idle經過kernel_thread建立,並始終運行在內核空間,負責全部內核進程的調度和管理。
它的任務就是管理和調度其餘內核線程kernel_thread, 會循環執行一個kthread的函數,該函數的做用就是運行kthread_create_list全局鏈表中維護的kthread, 當咱們調用kernel_thread建立的內核線程會被加入到此鏈表中,所以全部的內核線程都是直接或者間接的以kthreadd爲父進程 。
init進程由idle經過kernel_thread建立,在內核空間完成初始化後,加載init程序
在這裏咱們就主要講解下init進程,init進程由0進程建立,完成系統的初始化,是系統中全部其餘用戶進程的祖先進程
Linux中的全部進程都是由init進程建立並運行的。首先Linux內核啓動,而後在用戶空間中啓動init進程,再啓動其餘系統進程。在系統啓動完成後,init將變成爲守護進程監視系統其餘進程。
因此說init進程是Linux系統操做中不可缺乏的程序之一,若是內核找不到init進程就會試着運行/bin/sh,若是運行失敗,系統的啓動也會失敗。ssh
init服務的配置文件是/etc/inittab
在centos7以前inittab的配置文件是這樣的ide
# inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:5:initdefault:
裏面介紹了init的6個運行級別
0是關機函數
1是單用戶post
2是多用戶,不聯網this
3是多用戶centos7
4是不使用的操作系統
5是xwindows,也就是有界面的命令行
6是重啓
init命令很簡單。直接輸入init + 你想要的模式 回車就行。
咱們可使用runlevel命令來查詢當前系統的運行級別。
好比 輸入 : init 0 就是關機
init 3 就是切換到多用戶
init 5 就是切換到界面
init 6 就是重啓
可是千萬不要把initdefault設置爲0或者6
可是在centos7以後有了一個新的服務systemd取代了init,
systemd 是 Linux 下一個與 SysV 和 LSB 初始化腳本兼容的系統和服務管理器。在這裏我也不過多介紹了,你們有興趣能夠自行研究下。
init的運行級別配置是在/etc/init,而這些級別的定義是在/etc/rc.d目錄內的以下:
[root@centos6 rc.d]# ll total 60 drwxr-xr-x. 2 root root 4096 Jan 9 02:30 init.d -rwxr-xr-x. 1 root root 2617 Mar 23 2017 rc drwxr-xr-x. 2 root root 4096 Jan 9 02:30 rc0.d drwxr-xr-x. 2 root root 4096 Jan 9 02:30 rc1.d drwxr-xr-x. 2 root root 4096 Jan 9 18:39 rc2.d drwxr-xr-x. 2 root root 4096 Jan 9 18:39 rc3.d drwxr-xr-x. 2 root root 4096 Jan 9 18:39 rc4.d drwxr-xr-x. 2 root root 4096 Jan 9 18:39 rc5.d drwxr-xr-x. 2 root root 4096 Jan 9 02:30 rc6.d -rwxr-xr-x. 1 root root 220 Mar 23 2017 rc.local -rwxr-xr-x. 1 root root 20199 Mar 23 2017 rc.sysinit
這裏的rc{0..6}.目錄對應相應的級別裏面放的都是要啓動和關閉的進程咱們進去看一下
[root@centos6 rc3.d]# ls K01smartd K69rpcsvcgssd K95firstboot S13irqbalance S26udev-post K02oddjobd K73winbind K99rngd S13rpcbind S28autofs K05wdaemon K74ntpd S01sysstat S15mdmonitor S50bluetooth K10psacct K75ntpdate S02lvm2-monitor S22messagebus S55sshd K10saslauthd K75quota_nld S05rdma S23NetworkManager S80postfix K15htcacheclean K76ypbind S08ip6tables S24nfslock S82abrtd K15httpd K84wpa_supplicant S08iptables S24rpcgssd S83abrt-ccpp K30spice-vdagentd K87restorecond S10network S25blk-availability S90crond K50dnsmasq K88sssd S11auditd S25cups S95atd K50kdump K89netconsole S11portreserve S25netfs S99certmonger K60nfs K89rdisc S12rsyslog S26acpid S99local K61nfs-rdma K92pppoe-server S13cpuspeed S26haldaemon
這裏以K開頭的都是要關閉的進程,而以S開頭的則是要啓動的進程
[root@centos6 rc6.d]# ls K01certmonger K25sshd K74haldaemon K84wpa_supplicant K90network K01smartd K30postfix K74ntpd K85mdmonitor K92ip6tables K02oddjobd K30spice-vdagentd K75blk-availability K85messagebus K92iptables K05atd K50dnsmasq K75netfs K87irqbalance K92pppoe-server K05wdaemon K50kdump K75ntpdate K87restorecond K95firstboot K10cups K60crond K75quota_nld K87rpcbind K95rdma K10psacct K60nfs K75udev-post K88auditd K99cpuspeed K10saslauthd K61nfs-rdma K76ypbind K88rsyslog K99lvm2-monitor K15htcacheclean K69rpcsvcgssd K83bluetooth K88sssd K99rngd K15httpd K72autofs K83nfslock K89netconsole K99sysstat K16abrt-ccpp K73winbind K83rpcgssd K89portreserve S00killall K16abrtd K74acpid K84NetworkManager K89rdisc S01reboot
像rc6.d目錄中基本都是要關閉的進程,只有S00killall和S01reboot這兩個要啓動的進程他們分別是結束全部進程和重啓系統。這裏文件中的數字表明瞭他們的優先級,數字越小優先啓動。因此咱們本身作的服務放在這個目錄中時要謹慎以避免由於他所需的關聯程序沒有啓動而致使進程沒法啓動。
PS:若是真的不當心把init默認運行級別設置爲0或6的解決辦法
咱們知道init0和6級別分別對應的是關機和重啓,若是把這兩個設爲默認運行級別咱們是沒法進入系統的,因此咱們就要藉助救援系統了,在開機GRUB界面按e以下:
選擇kernel這行接着按e
在命令行最後輸入1(進入單用戶模式),回車退後到上個界面
接着按b進入單用戶模式,咱們這就進入到單用戶模式了
咱們只須要進入/etc/inittab配置文件中把最後的0或6改成3,重啓系統就能夠啦