大家知道在作業系統中,服務是通過程序提供的。是以了解Linux系統的程序運作情況,能使我們對主機提供更好的管理機制。
(一)程序的管理
1,如何檢視程序:
(1)通過ps aux或ps -elf指令檢視某一時刻的程序狀态
[[email protected] ~]# ps aux |head
USER PID%CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 10372 696 ? Ss 20:10 0:00 init [5]
root 2 0.0 0.0 0 0 ? S< 20:10 0:00 [migration/0]
root 3 0.0 0.0 0 0 ? SN 20:10 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S< 20:10 0:01 [events/0]
root 5 0.0 0.0 0 0 ? S< 20:10 0:00 [khelper]
root 14 0.0 0.0 0 0 ? S< 20:10 0:00 [kthread]
root 18 0.0 0.0 0 0 ? S< 20:10 0:00 [kblockd/0]
root 19 0.0 0.0 0 0 ? S< 20:10 0:00 [kacpid]
root 192 0.0 0.0 0 0 ? S< 20:10 0:00 [cqueue/0]
(2)通過top指令動态檢視程序
P、M:根據 %CPU、%MEM 降序排列
T:根據程序消耗的 TIME降序排列
k、r:殺死指定的程序、重設程序優先級
q:退出 top 程式
[[email protected] ~]#top |head -n 15
top - 21:08:51 up 58 min, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 169 total, 1 running, 167 sleeping, 0stopped, 1 zombie
Cpu(s): 0.6%us, 0.7%sy, 0.1%ni, 96.4%id, 2.0%wa, 0.0%hi, 0.3%si, 0.0%st
Mem: 1023844ktotal, 687024k used, 336820k free, 41960k buffers
Swap: 2096472ktotal, 0k used, 2096472k free, 388836k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 10372 696 588 S 0.0 0.1 0:00.81 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0
3 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
4 root 10 -5 0 0 0 S 0.0 0.0 0:01.79 events/0
5 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khelper
14 root 11 -5 0 0 0 S 0.0 0.0 0:00.00 kthread
18 root 10 -5 0 0 0 S 0.0 0.0 0:00.05 kblockd/0
19 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kacpid
(3)pgrep 根據特定條件篩選
-l PID連同程序名一起輸出
-U 檢索指定使用者的程序
-t 檢索指定終端的程序
[[email protected] ~]# pgrep -l-U root
1 init
2 migration/0
3 ksoftirqd/0
4 events/0
5 khelper
14 kthread
18 kblockd/0
………
(4)pstree 樹狀結構檢視
-a 顯示完整指令行
-u 列出各程序所屬的使用者名
-p 列出對應的PID号
2,如何管理程序:以xsnow為例
(1)安裝此款軟體
[[email protected] Desktop]# yumlocalinstall -y xsnow-1.42-10.i386.rpm --nogpgcheck
Loaded plugins: product-id, security,subscription-manager
This system is not registered to Red Hat SubscriptionManagement. You can use subscription-manager to register.
Setting up Local Package Process
Examining xsnow-1.42-10.i386.rpm: xsnow-1.42-10.i386
Marking xsnow-1.42-10.i386.rpm to be installed
Resolving Dependencies
--> Running transaction check
………………………………………
……………..
(2)打開軟體,發現軟體在前台運作時不能做其他任何操作
[[email protected] Desktop]# xsnow
Xsnow-1.42, December 14th 2001 by Rick Jansen([email protected])
WWW: http://www.euronet.nl/~rja/Xsnow/
(3)Ctrl+Z使目前運作停止并放置到背景,檢視背景運作程序
[[email protected] ~]# xsnow
Xsnow-1.42, December 14th 2001 by Rick Jansen([email protected])
WWW: http://www.euronet.nl/~rja/Xsnow/
[1]+ Stopped xsnow
[[email protected] ~]# jobs
[1]+ Stopped xsnow
[[email protected] ~]#
(4)把背景程序排程到前台
[[email protected] ~]# fg xsnow
Xsnow
(5)直接把背景程序放到背景運作
[[email protected] ~]#bg xsnow
[1]+ xsnow &
(6)在程式後面加&可使直接轉入背景運作,此時可以操作主機
[[email protected] Desktop]# xsnow&
[1] 7653
[[email protected] Desktop]# Xsnow-1.42, December 14th2001 by Rick Jansen ([email protected])
WWW: http://www.euronet.nl/~rja/Xsnow/
[[email protected] Desktop]#ls
xsnow-1.42-10.i386.rpm
(7)打開多個xsnow程式
(8)檢視背景程序
[[email protected] ~]# jobs
[1] Running xsnow&
[2] Running xsnow&
[3] Running xsnow&
[4] Running xsnow&
[5] Running xsnow&
[6]- Running xsnow&
[7]+ Running xsnow&
(9)一次性結束xsnow程序
[[email protected] ~]# killallxsnow
[1] Done xsnow
[2] Done xsnow
[3] Done xsnow
[4] Done xsnow
[5] Done xsnow
[6]- Done xsnow
[7]+ Done xsnow
[[email protected] ~]#
程序的管理大體為程序的控制和終止
程序控制
& 放入背景運作,運作在記憶體中的程序
ctrl +z 将目前的作業放入背景并暫停運作
jobs 檢視背景程序
fg 編号把背景程序調到前台
bg 編号讓程式在背景運作
終止程序
ctrl +c 終止目前正在運作的程序
kill -9 pid 強制殺掉程序
killall 程序名殺死開啟多個的同一程序
pkill [-9] 程序名[也可以是使用者]
(二)服務的管理
1,服務的分類:服務分為系統服務和臨時服務兩大類
獨立系統服務:響應速度快、占用系統資源;
臨時服務:響應速度慢、較節省系統資源;
2,服務狀态:chkconfig--list 顯示全部服務的啟動狀态
chkconfig --list xxx 顯示xxx服務的啟動狀态
差別已安裝過的服務是系統服務還是臨時服務
rpm -ql httpd | grep init/xinetd
[[email protected] ~]# chkconfig --list | head
NetworkManager 0:off 1:off 2:off 3:off 4:off 5:off 6:off
acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off
anacron 0:off 1:off 2:on 3:on 4:on 5:on 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[[email protected] ~]# chkconfig --list autofs
autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[[email protected] ~]# rpm -qlautofs | grep xinetd
[[email protected] ~]#rpm -qlautofs | grep init
/etc/rc.d/init.d/autofs
3, 如何差別系統服務和臨時服務
(1)看路徑:系統服務 /etc/init.d/ 臨時服務/etc/xinetd.d/
(2)用chkconfig list 全部列舉,可以根據标題看出服務類型
chkconfig list 服務:列舉指定的某個服務狀态
4,手動啟動系統服務的兩種方法
(1)service 系統服務 stop/restart/start/status/reload
[[email protected] ~]# service autofs restart
Stopping automount: [ OK ]
Starting automount: [ OK ]
(2)/etc/init.d/系統服務 start/stop/restart/status/reload
[[email protected] ~]# /etc/init.d/autofs restart
Stopping automount: [ OK ]
Starting automount: [ OK ]
5,自動啟動系統服務的兩種方法
(1)chkconfig 服務 on/off 預設級别2345
chkconfig --level 35系統服務on/off指定服務級别
[[email protected] ~]# chkconfig --list NetworkManager
NetworkManager 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[[email protected] ~]# chkconfig --level 35NetworkManager on
[[email protected] ~]# chkconfig --list NetworkManager
NetworkManager 0:off 1:off 2:off 3:on 4:off 5:on 6:off
[[email protected] ~]#
(2)ntsysv --level 35 系統服務指定服務級别
空格選擇開啟/關閉服務
Tab ok确定
[[email protected] ~]# chkconfig --list NetworkManager
NetworkManager 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[[email protected] ~]# ntsysv --level 35
[[email protected] ~]# chkconfig --list NetworkManager
NetworkManager 0:off 1:off 2:off 3:on 4:off 5:on 6:off
[[email protected] ~]#
6,臨時服務開啟的兩種方法
首先需要安裝并打開xinetd托管服務
[[email protected]~]#rpm -qi xinetd
package xinetd is not installed
[[email protected]~]# yum install -y xinetd
Failedto set locale, defaulting to C
Loadedplugins: product-id, security, subscription-manager
Thissystem is not registered to Red Hat Subscription Management. You can usesubscription-manager to register.
Settingup Install Process
ResolvingDependencies
-->Running transaction check
--->Package xinetd.x86_64 2:2.3.14-17.el5 set to be updated
-->Finished Dependency Resolution
………………………………………..
………………………….
[[email protected]~]#chkconfig --level 35 xinetd on
[[email protected]~]#chkconfig --list xinetd
xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
(1)chkconfig 臨時服務 on
[[email protected] ~]# chkconfig --list rsync
rsync off
[[email protected] ~]# chkconfig rsync on
[[email protected] ~]# chkconfig --list rsync
rsync on
(2)vim /etc/xinetd.d/臨時服務
把 disable改為no
[[email protected] ~]# chkconfigrsync off
[[email protected] ~]# vim/etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition toan ftp server, as it \
# allowscrc checksumming etc.
service rsync
{
disable= no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
~
~
-- INSERT -- 6,14-21 All
[[email protected] ~]# chkconfig--list rsync
rsync on