天天看點

Linux基礎:調查和管理程序

什麼是程序?

程序是被載入記憶體的一個指令集合

  程序ID(Process ID,PID)号碼被用來标記各個程序

  UID、GID相關權限和 SElinux決定對檔案系統的存取和通路

  通常從執行程序的使用者處繼承

檢視程序

使用ps來檢視程序資訊

  僅執行ps,顯示目前終端中的程序

  a 選項包括與現行終端的所有程序

  x 選項包括不連接配接終端的程序

  u 選項顯示程序所有者的資訊

  f 選項顯示程序的父程序

  o 屬性,…選項顯示定制的資訊:

  ps  axo  

  pid,comm,%cpu,%mem,state,tty,ppid,pri,ni,user,cmd

  ps aux  ;  ps -l

搜尋程序

最靈活:ps 選項 | 其它指令

ps axo comm,tty | grep ttyS0

按預定義的模式:pgrep

$ pgrep -U uid    sshd

$ pgrep -G gid     sshd

按确切的程式名稱:pidof :找出一個程式的pid

$ pidof bash

Ps選項  

預設顯示目前終端中的程序

-a 選項包括所有終端中的程序

-x 選項包括不連接配接終端的程序

-u 選項顯示程序所有者的資訊

-f 選項顯示程序的父程序

-e選項是現實所有程序

-p pid 顯示些pid的程序資訊

-o 屬性,…選項顯示定制的資訊:

pid、comm、%cpu、%mem、state、tty、euser、ruser

ps  -e   兩個都是檢視所有程序

ps  -ef

ps  axo comm,tty|grep tty

<a href="http://blog.51cto.com/attachment/201308/182220961.png" target="_blank"></a>

pgrep  -U root ssh

<a href="http://blog.51cto.com/attachment/201308/182220572.png" target="_blank"></a>

pgrep –G root |more

<a href="http://blog.51cto.com/attachment/201308/182222523.png" target="_blank"></a>

查找cups程序

<a href="http://blog.51cto.com/attachment/201308/182226287.png" target="_blank"></a>

産看程序id号

<a href="http://blog.51cto.com/attachment/201308/182226941.png" target="_blank"></a>

通過一下選項顯示程序的詳細資訊

<a href="http://blog.51cto.com/attachment/201308/182228197.png" target="_blank"></a>

列出程序調用bash的程序号

<a href="http://blog.51cto.com/attachment/201308/182229542.png" target="_blank"></a>

或者通過pidof 來檢視bash程序

<a href="http://blog.51cto.com/attachment/201308/182231638.png" target="_blank"></a>

信号

最基本的程序間通訊方式

  -直接發送給程序,不需要使用者界面

  -程式給每個信号關關聯作

  -發送信号可以使用名稱或号碼:

    信号15 ,TERM(預設):正常終止

    信号9,KILL:強制立即終止

    資訊1,HUP:重讀配置檔案

    man 7 signal 顯示了完整清單資訊,比較詳細

通過kill -l 來檢視信号類型,但是資訊沒有那麼詳細

<a href="http://blog.51cto.com/attachment/201308/182233495.png" target="_blank"></a>

給程序發送信号

按PID:kill [-信号] pid …

按名稱:killall [-信号] bash

按模式:pkill [-信号] 模式 

例如檢視rsyslogd程序号,然後通過kill重新啟動

<a href="http://blog.51cto.com/attachment/201308/182237356.png" target="_blank"></a>

通過tail指令檢視是否重新啟動

<a href="http://blog.51cto.com/attachment/201308/182248960.png" target="_blank"></a>

例子1:把SIGHUP指令發送給rsyslogd

#killall -1 rsyslogd

例子2:強制終止httpd程序

#killall -9 httpd

排程優先性

排程優先級priority決定對CPU的使用順序

優先級受程序的nice值(nice value)影響

這個nice值的範圍是-20到19,預設為0

   -pri值越小,說明對CPU的使用越優先

使用ps –o comm,nice ;ps axo pid,nice,user

ps -l 指令可以檢視

<a href="http://blog.51cto.com/attachment/201308/182252355.png" target="_blank"></a>

改變排程優先性

nice值可以被改變…

  -在啟動程序時:

      nice  -n +5     指令名字

      nice   -n -10    指令名字

  -在啟動後:

     $ renice  +5   PID     ;  renice  -5   PID  

     $ renice  -n +5   PID     ;  renice -n  -5   PID

隻有根使用者才能降低nice值(提高優先性)        nice -n -10 vim                      nice -n +10 vim 

普通使用者的nice隻能是0-19,而且每隻能比前一次大

檢視comm,和nice

啟動程序時給定nice值:nice –n 5 指令

<a href="http://blog.51cto.com/attachment/201308/182312963.png" target="_blank"></a>

修改bash的nice值

<a href="http://blog.51cto.com/attachment/201308/182317434.png" target="_blank"></a>

修改bash的nice值為15 renice –n 15  pid

<a href="http://blog.51cto.com/attachment/201308/182321188.png" target="_blank"></a>

檢視bash的nice值是否發生變化

<a href="http://blog.51cto.com/attachment/201308/182328251.png" target="_blank"></a>

修改vim的nice值

<a href="http://blog.51cto.com/attachment/201308/182332968.png" target="_blank"></a>

通過例子可以看到root可以使nice值變小,也可将變小的值提高

<a href="http://blog.51cto.com/attachment/201308/182335116.png" target="_blank"></a>

檢視普通使用者能否修改nice值

<a href="http://blog.51cto.com/attachment/201308/182941921.jpg" target="_blank"></a>

互動式程序管理工具

動态檢視程序資訊的指令行:top

(資訊)檢視rhce-||調查和管理程序.doc文檔

關于top指令的用法

M:以記憶體使用為序排列

P:以cpu的使用率排列

h:幫助

q(小寫):退出

  圖形化界面:gnome-system-monitor

  能力

   -顯示實時程序資訊

   -允許排序、中止運作、和重設nice值

Gnome-system-monitor

<a href="http://blog.51cto.com/attachment/201308/182406887.png" target="_blank"></a>

作業控制

前台與背景差別

在背景運作程序

-在指令行後添加一個和号(&amp;): firefox&amp;

将程序放到背景中暫時停止:

-使用Ctrl -z 或發送信号 19 (STOP)

管理背景作業或暫停的作業

-列舉作業号碼和名稱:jobs

-在背景恢複運作:bg  [%作業号碼]

-在前台恢複運作:fg   [%作業号碼]

-發送信号:kill  -[信号]  [%作業号碼]

計劃任務與例行任務

一次性作業使用at,重複性作業使用crontab

<a href="http://blog.51cto.com/attachment/201308/182409482.png" target="_blank"></a>

沒有被重導向的輸出會被郵寄給使用者

根使用者能夠修改其它使用者的作業

*   The  superuser  may  use  these commands in any case.  For other users, permission to use at is determined by the

*   files /etc/at.allow and /etc/at.deny.

*   If the file /etc/at.allow exists, only usernames mentioned in it are allowed to use at.

*   If /etc/at.allow does not exist, /etc/at.deny is checked, every username not mentioned in it is then  allowed  to

*   use at.

*   If neither exists, only the superuser is allowed use of at.

*   An empty /etc/at.deny means that every user is allowed use these commands, this is the default configuration.

與at有關的配置檔案,at.allow,at.deny

at.allow優先級比at.deny的高:

 1.先看at.allow,有在裡面的使用者可以使用at

  at.allow不存在時,則檢查at.deny,裡面的使用者不能使用

 2.兩個檔案不存在則隻有root才可以使用

 一般系統預設隻有一個空的at.deny,意味着系統裡所有的使用者都可以使用at,

  兩個檔案裡寫使用者裡一行一個使用者

步驟

1、先service atd start|restart

2、at   時間 

時間:at  now + 20 minutes

             at  20:39 2013-4-13

回車後,可以輸入要執行的指令

輸入完成後ctrl+d

atq = at -l   檢視

atrm 作業号= at -d   作業号

Crontab檔案格式

項目包含五個用空格分隔的字段,然後是要執行的指令

每行一個項目,行的長度沒有限制

字段是:分鐘、小時、日期、月份、星期幾

注釋行以#開頭

詳細參見man 5 crontab的說明書頁

與crontab有關的配置檔案:cron.allow、cron.deny

1.先看cron.allow,在裡面的使用者則可以使用crontab

2.沒有cron.allow的,檢查cron.deny,在裡面的不可以使用crontab

3.兩個檔案都不存在時隻有root可以使用

一般系統預設隻有一個空的cron.deny,意味着所有人都可以使用

以上兩個檔案一行一個使用者賬号

步驟:

 1  /etc/init.d/crond  start|restart或者service crond start|restart

  2  crontab -e 

  3  出現的vi編輯器輸入要執行的指令,一行一個任務,輸入完了:wq儲存退出

檢視:crontab -l 

 删除此使用者所有任務:crontab -r

隻想修改,crontab -e

/etc/crontab

系統自動周期性執行

vim /etc/crontab

      SHELL=/bin/sh

       MAILTO=paul

       # run five minutes after midnight, every day

       5 4 3 6 *       $HOME/bin/daily.job &gt;&gt; $HOME/tmp/out 2&gt;&amp;1

       # run at 2:15pm on the first of every month -- output mailed to paul

       15 */2  1 *   *     $HOME/bin/monthly

       # run at 10 pm on weekdays, annoy Joe

      0 22 * * 1-5    mail -s "It’s 10pm" joe%Joe,%%Where are your kids?%

       23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"      5 4 * * sun     echo "run at 5 after 4 every sunday"

聚集指令

聚集指令概念:把指令寫在一起

有兩種聚集指令的方法:

  -複合式:date; who | wc –l

     指令會一個接一個地運作

子shell:(date;who | wc –l)&gt;&gt; /tmp/trace

  -所有的輸出都被發送給單個STDOUT和STDER

例子

<a href="http://blog.51cto.com/attachment/201308/182410260.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201308/182411381.png" target="_blank"></a>

退出狀态

程序使用退出狀态來報告成功或失敗

-0代表成功,1-255代表失敗

-$? 變量儲存上一條的指令退出狀态

-exit [num]:終止,并把狀态設定為num

-Shell腳本可以自定義錯誤資訊

例如:

 $ ping -c 1 -W 1 station999 &amp;&gt; /dev/null 

 $ echo $?

<a href="http://blog.51cto.com/attachment/201308/182413126.png" target="_blank"></a>

正常執行echo $?傳回0 ,錯誤的話傳回大于零的數字

條件性的執行操作符

根據退出狀态而定,指令可以有條件地運作

&amp;&amp; 代表條件性的AND THEN

&amp;&amp;隻有第一條指令隻有執行成功後,才能那個執行第二條指令

  ||  代表條件性的OR ELSE

||隻有第一條指令執行失敗後,才能執行第二條指令

 $ grep -q no_such_user /etc/passwd || echo 'No such user' 

 No such user

 $ ping -c1 -W2 station1 &amp;&gt; /dev/null  \

 &gt;    &amp;&amp; echo "station1 is up"         \

 &gt;    || (echo 'station1 is unreachable'; exit 1) 

 station1 is up 

test 指令

判斷條件是否成立,以便用在條件性執行中

條件成立,則為真,則傳回0

條件不成立,則為假,則傳回非0

長格式的例子:

  $ test "$A" = = "$B" &amp;&amp; echo "Strings are equal" 

  $ test "$A" -eq "$B" &amp;&amp; echo "Integers are equal" 

簡寫格式的例子:

  $ [   "$A"   ==    "$B"    ] &amp;&amp; echo "Strings are equal“

  $ [   "$A"   -eq  "$B"   ] &amp;&amp; echo "Integers are equal" 

<a href="http://blog.51cto.com/attachment/201308/182415485.png" target="_blank"></a>

檢視日志的重要性

日志的作用

  解決系統故障

  解決網絡服務問題

  記錄登陸資訊

常見的日志檔案

/var/log/secure 記錄使用者登入系統,隻有有要求輸入密碼的就紀錄

/var/log/wtmp 記錄登陸者的資訊資料,由于這個檔案已編碼,隻能使用last指令來檢視

/var/log/messages 這個檔案相當重要,隻是系統的錯誤資訊都會記錄在這個檔案中

/var/log/boot.log 記錄開機或一些服務啟動和關閉資訊

/var/log/maillog 和/var/log/mail/*記錄郵件來往的使用者記錄

/var/log/cron 記錄crontab例行性服務的内容

/var/log/httpd ,/var/log/news /varlog/samba 等記錄網絡服務的檔案

     本文轉自Tar0 51CTO部落格,原文連結:http://blog.51cto.com/tar0cissp/1275495,如需轉載請自行聯系原作者

繼續閱讀