天天看點

Linux開關機及系統程序指令

【一】Linux 系統啟動流程

Linux開關機及系統程式指令

【二】關于Linux開機與關機

# 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:3:initdefault:

runlevel Active
系統關機
1 單使用者模式
2 本地多使用者模式(無法使用遠端網絡)
3 網絡多使用者模式
4 系統保留未定義
5 帶圖形界面的網絡多使用者模式
6 系統重新開機

init 指令:init + runlevel進行相應操作的切換

【三】Linux如何關閉和重新開機

Linux是一個多使用者多任務的系統,關機時,需要注意一下事項:

(1)觀察系統的使用狀态,如誰線上,線上的使用者在幹什麼(w)

(2)通知線上使用者關機的相關資訊(可選)(wall,write)

(3)使用最安全的的關機方式(執行關機指令前執行sync将記憶體資料寫入磁盤)

Linux系統安全關機的過程:

(1)執行sync指令将記憶體資料寫入磁盤;

(2)關機指令下發,通知使用者關機;

(3)停止系統的所有程序;

(4)解除安裝系統挂載的裝置

【四】Linux系統關機常用指令

(1)halt :立刻關機

(2)poweroff :立刻關機

(3)shutdown -h now 立刻關機(root使用者使用)

(4)shutdown -h 10  10分鐘後自動關閉

注:在使用shutdown 指令設定重新開機的話,shutdown -c 将取消重新開機

【五】Linux系統開機常用指令

(1)reboot

(2)shutdown -r now 立刻重新開機(root使用者使用)

(3)shutdown -r 10 過10分鐘後重新開機

(4)shutdown -r 20:35 在20:35時重新啟動(root使用者使用)

【六】系統資訊和程序類指令

指令 說明
man 獲得指令的幫助手冊,如:man cp;按q鍵退出
su 切換使用者,如 su -   root;"-" 表示改變使用者的環境變量
who 顯示系統中登入的使用者
w 顯示登入使用者的詳細資訊
last 檢視最近登入系統的使用者資訊
date 系統日期時間的設定,一般要結合參數-s使用
uname 檢視系統的版本 ,如uname -R
reboot 重新開機計算機,如reboot;shutdown -r   now
shutdown 關閉計算機;如:shutdown -h now 表示停止服務後再關閉
free 檢視記憶體和swap分區情況
ps 顯示程序資訊,參數-ux為目前使用者的程序;參數-aux為所有使用者的程序,參數-ef為系統所有程序資訊
uptime 顯示目前時間,系統開機運轉時間,連接配接數,最近1,5,15分鐘的系統負載。(1個CPU  一般不超過0.8)
top 檢視系統CPU、記憶體等使用情況
kill 删除某個程序,程序号可用ps指令獲得,如kill - 9   121
vmstat 檢視虛拟記憶體的使用情況
nohup 将程序在使用者登出後仍舊繼續執行
jobs 檢視被挂起或背景運作的程序
Ctrl + Z 程序挂起
Ctrl + C 程序終止
fg 程序恢複到前台繼續執行
bg 程序恢複到背景繼續執行

【七】涉及到的指令操作示範

【實戰一】 Linux 開關機

【關機】

halt  :系統關閉,電源仍然工作

poweroff :關閉電源 icpi

shutdown -h:調用poweroff,halt指令

shutdown -h 0

shutdonw -h now

【重新開機】

shutdown -r

shutdown -r 10

shutdown -r 20:45

(1)shutdown --help

Options:

  -r                          reboot after shutdown

  -h                          halt or power off after shutdown

  -H                          halt after shutdown (implies -h)

  -P                          power off after shutdown (implies -h)

  -c                          cancel a running shutdown

  -k                          only send warnings, don't shutdown

  -q, --quiet                 reduce output to errors only

  -v, --verbose               increase output to include informational messages

      --help                  display this help and exit

      --version               output version information and exit

(2)

[root@db01 /]# halt

Broadcast message from root@db01

        (/dev/pts/0) at 14:27 ...

The system is going down for halt NOW!

(3)

shutdown -r now

        (/dev/pts/0) at 14:35 ...

The system is going down for reboot NOW!

(4)

[root@db01 ~]# shutdown -h 20:30

        (/dev/pts/0) at 14:36 ...

The system is going down for halt in 354 minutes!

(5)[root@db01 ~]# shutdown -c

^Hshutdown: Shutdown cancelled

(6) shutdown -k 1  "The system is going down  for maintenance in 1 minute"

        (/dev/pts/0) at 14:40 ...

The system is going down for maintenance in 1 minute!

The system is going down  for maintenance in 1 minute

(7)shutdown -t 60 -h now  "The system is going down  for maintenance in 1 minute"

        (/dev/pts/0) at 14:41 ...

【實戰二】 Linux 程序管理指令

常用指令:

(1)[root@db01 ~]# w

 14:47:11 up 4 min,  1 user,  load average: 0.00, 0.02, 0.00

USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT

root     pts/0    10.53.110.78     14:42    0.00s  0.05s  0.01s w

注:遠端登入pts,本地登入tty

(2)[root@db01 ~]# w

 14:50:10 up 7 min,  2 users,  load average: 0.00, 0.00, 0.00

root     pts/0    10.53.110.78     14:42    0.00s  0.04s  0.00s w

root     pts/1    10.53.110.78     14:49    4.00s  0.02s  0.01s top

 ps

ps -aux

(3)[root@db01 ~]# ps -aux|more

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1  0.2  0.0  19364  1532 ?        Ss   14:42   0:01 /sbin/init

root         2  0.0  0.0      0     0 ?        S    14:42   0:00 [kthreadd]

root         3  0.0  0.0      0     0 ?        S    14:42   0:00 [migration/0]

root         4  0.0  0.0      0     0 ?        S    14:42   0:00 [ksoftirqd/0]

(4)[root@db01 ~]# ps -aux --sort time|more   ## 按照時間排序

root         1  0.1  0.0  19364  1532 ?        Ss   14:42   0:01 /sbin/init

root         5  0.0  0.0      0     0 ?        S    14:42   0:00 [migration/0]

root         6  0.0  0.0      0     0 ?        S    14:42   0:00 [watchdog/0]

root         7  0.0  0.0      0     0 ?        S    14:42   0:00 [migration/1]

root         8  0.0  0.0      0     0 ?        S    14:42   0:00 [migration/1]

root         9  0.0  0.0      0     0 ?        S    14:42   0:00 [ksoftirqd/1]

(4)[root@db01 ~]# ps -aux --sort pid|more    ##按pid排序

【注】VSZ:使用虛拟記憶體大小

RSS:使用實際記憶體大小

TTY:終端類型

STAT:    R:運作程序   S:睡眠程序 I:空閑基礎 Z:僵死程序 D:不可中斷程序  T:終止程序  P:交換程序

(5)[root@db01 ~]# ps -A|more  ##顯示所有程序

  PID TTY          TIME CMD

    1 ?        00:00:01 init

    2 ?        00:00:00 kthreadd

    3 ?        00:00:00 migration/0

    4 ?        00:00:00 ksoftirqd/0

    5 ?        00:00:00 migration/0

    6 ?        00:00:00 watchdog/0

    7 ?        00:00:00 migration/1

    8 ?        00:00:00 migration/1

    9 ?        00:00:00 ksoftirqd/1

   10 ?        00:00:00 watchdog/1

   11 ?        00:00:00 events/0

(6)[root@db01 ~]# ps -u root |more  ##檢視某個使用者的程序

(7)

[root@db01 ~]# ps -ef|more

UID        PID  PPID  C STIME TTY          TIME CMD

root         1     0  0 14:42 ?        00:00:01 /sbin/init

root         2     0  0 14:42 ?        00:00:00 [kthreadd]

root         3     2  0 14:42 ?        00:00:00 [migration/0]

root         4     2  0 14:42 ?        00:00:00 [ksoftirqd/0]

root         5     2  0 14:42 ?        00:00:00 [migration/0]

root         6     2  0 14:42 ?        00:00:00 [watchdog/0]

root         7     2  0 14:42 ?        00:00:00 [migration/1]

root         8     2  0 14:42 ?        00:00:00 [migration/1]

root         9     2  0 14:42 ?        00:00:00 [ksoftirqd/1]

root        10     2  0 14:42 ?        00:00:00 [watchdog/1]

UID :使用者ID

PID:程序ID

PPID:父程序ID

C:0級,沒有子程序

STIME:運作時間

(8)

[root@db01 ~]# ps -ef|grep  sshd

root      1508     1  0 14:42 ?        00:00:00 /usr/sbin/sshd

root      1693  1508  0 14:42 ?        00:00:00 sshd: root@pts/1,pts/2

root      1884  1848  0 15:08 pts/2    00:00:00 grep sshd

(9)根據CPU和mem排序 (前5行)

[root@db01 ~]# ps -aux --sort -pcpu,-pmem|head -5

root      1767  0.1  0.0  15036  1212 pts/1    S+   14:50   0:01 top

root         1  0.0  0.0  19364  1532 ?        Ss   14:42   0:01 /sbin/init

(10)顯示父程序和子程序

[root@db01 ~]# ps -C sshd   #檢視程序

 1508 ?        00:00:00 sshd

 1693 ?        00:00:00 sshd

[root@db01 ~]# ps -p 1508 -L  #檢視程序有多少線程

  PID   LWP TTY          TIME CMD

 1508  1508 ?        00:00:00 sshd

[root@db01 ~]# ps --ppid 1693  #檢視程序的子程序

 1737 pts/1    00:00:00 bash

 1848 pts/2    00:00:00 bash

kill指令

SYNOPSIS

       kill [-s signal|-p] [--] pid...

       kill -l [signal]

(1)kill的信号

[root@db01 ~]# kill -l

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP

 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1

 15) SIGTERM

[root@db01 ~]# ps -ef|grep top

root      2011  1984  0 15:20 pts/1    00:00:00 top

root      2015  1921  0 15:20 pts/0    00:00:00 grep top

[root@db01 ~]#

[root@db01 ~]# kill -9  2011

root      2017  1921  0 15:21 pts/0    00:00:00 grep top

【注】程序号為1不能kill,否則系統不能使用

nohup指令

(1)[root@db01 ~]# nohup vmstat 2>1 1.log &  ##在背景運作