正確的關機

  1. Linux 為多人多工環境,螢幕背後可能有多人或多個服務正在工作,不正常關機可能造成檔案系統毀損。
  2. sync:手動將緩衝於記憶體內的磁碟資料寫入磁碟。
    1. Linux 系統運作效率的考量,記憶體會暫存一些該寫入硬碟的資料。
    2. 正常的運作下,系統運作會視需要將資料回寫入硬碟。
    3. 該寫入硬碟的資料,有時還在記憶體中,等待回寫。因此,Linux 不能任意斷電關機。
    4. 正常的關機程序如 shutdown, reboot, halt 等指令都會呼叫特殊的 system call: sync, 來進行記憶體資料回寫的同步作業。以確保資料的正確。
  3. who:查看誰在線上。
    [root@dywHome2 ~]# who
    dywang   tty1         2008-09-07 06:00
    
  4. ps -aux:查詢正在進行的程序。
  5. netstat -a:查看網路連線狀態。
  6. 正確關機指令:shutdown, poweroff, halt 及 reboot。
    [root@dywOffice bin]# cd /sbin
    [root@dywOffice sbin]# ll halt shutdown poweroff reboot
    -rwxr-xr-x 1 root root 10540 Jul  2  2006 halt*
    lrwxrwxrwx 1 root root     4 Mar 14  2007 poweroff -> halt*
    lrwxrwxrwx 1 root root     4 Mar 14  2007 reboot -> halt*
    -rwxr-xr-x 1 root root 18736 Jul  2  2006 shutdown*
    
  7. shutdown:通知線上使用者關機的時刻後關機。
    [root@mdk-dyw ~]# shutdown
    Usage:    shutdown [-akrhfnc] [-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.
                      -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") **
    [root@mdk-dyw ~]# shutdown -h now
    [root@mdk-dyw ~]# shutdown -h 00:05
    [root@mdk-dyw ~]# shutdown -h +30
    [root@mdk-dyw ~]# shutdown -r now
    [root@mdk-dyw ~]# shutdown -r +30 'The system will reboot'
    [root@mdk-dyw ~]# shutdown -k now 'This system will reboot'
    
  8. halt:關機。
    [root@mdk-dyw ~]# halt
    
  9. reboot:重新開機。
    [root@mdk-dyw ~]# reboot
    

練習題

  1. Linux 關機前,如何查詢目前誰在線上?
    Sol. who
  2. 如何在文字界面底下正確的關機的指令為?
    Sol. halt, shutdown 或 poweroff
  3. 如何在文字界面底下正確的重新開機的指令為?
    Sol. reboot
  4. Linux 系統正確的關機的指令,會自動呼叫那個指令,將記憶體中的資料存回硬碟?
    Sol. sync
  5. Linux 系統正確的關機的指令 shutdown -h now,代表意義為何?
    Sol. 馬上關機。
  6. Linux 系統正確的關機的指令 shutdown -h 00:05,代表意義為何?
    Sol. 00:05 關機。
  7. Linux 系統正確的關機的指令 shutdown -h +30,代表意義為何?
    Sol. 30 分鐘後關機。
  8. Linux 系統正確的關機的指令 shutdown -r now,代表意義為何?
    Sol. 現在馬上重新開機。
  9. Linux 系統正確的關機的指令 shutdown -r +30 'This system will reboot',代表意義為何?
    Sol. 30 分鐘後重新開機,並送出警告訊息 This system will reboot 給線上使用者。
  10. Linux 系統正確的關機的指令 shutdown -k now 'This system will reboot',代表意義為何?
    Sol. 沒有真的關機,只是送出警告訊息 This system will reboot 給線上使用者。