進程管理相關知識
一 進程相關概念
進程:數據處理,管理數據,對數據進行計算。獨占資源,數據處理,一個進程可以有多個線程,也可以沒有;
線程:依附於進程運行,也是處理數據。線程之間共享資源;一個線程中不能有進程,而且線程中不允許有線程;可以啟動多個線程,但每次執行的結果不一樣
二 查看進程相關命令
1.ps
[plain]
[root@client01 ~]# ps
PIDTTY TIME CMD
1157pts/0 00:00:00 bash
1600pts/0 00:00:00 ps
#ps -ef包括的進程有:前台進程、後台進程、多個終端運行的進程
[root@client01 ~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 16:49 ? 00:00:00 /sbin/init
#UID:用戶編號
#PID:進程編號
#PPID:父進程編號
#STIME:開始啟動時間
#TTY:?表示後台進程
#TIME:占用CPU的時間
#CMD:啟動的命令
#[]:後台進程
[root@client01 ~]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 19204 1484 ? Ss 16:49 0:00 /sbin/init
#每一列代表什麼意思可以通過man手冊到
#init:該進程不能殺死
2.top:動態顯示進程的所有信息
[plain]
top - 19:37:19 up 2:47, 1 user, load average: 0.00, 0.00,0.00
Tasks: 73 total, 1 running, 72 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 384996k total, 148672kused, 236324k free, 10564k buffers
Swap: 1048568k total, 0kused, 1048568k free, 49440k cached
#僵屍進程:子進程死掉,父進程並不知道。父進程仍然分配資源,會對系統資源進行浪費。不能殺死。處理:重啟系統
#孤兒進程:父進程死掉,子進程就成了孤兒進程。不用管理。上一級進程進行管理(一般是init)。
#buffer(主要在內存中):主要解決速率不匹配問題,磁盤和內存之間的緩沖區
#cache(主要在CPU中):主要解決速率不匹配問題,CPU和內存的數據緩沖區
三 啟動和關閉進程
[plain]
#啟動一個進程
[root@client01 ~]# /etc/init.d/postfixstart
[root@client01 ~]# /etc/init.d/postfix stop
Shutting down postfix: [ OK ]
[root@client01 ~]# service postfix start
Starting postfix: [ OK ]
[root@client01 ~]# service postfix stop
Shutting down postfix: [ OK ]
[root@client01 ~]# service postfix restart
Shutting down postfix: [ OK ]
Starting postfix: [ OK ]
[root@client01 ~]# ps -ef|grep postfix
root 2145 1 0 13:34 ? 00:00:00 /usr/libexec/postfix/master
postfix 2147 2145 0 13:34 ? 00:00:00 pickup -l -t fifo -u
postfix 2148 2145 0 13:34 ? 00:00:00 qmgr -l -t fifo -u
root 2150 1157 0 13:34 pts/0 00:00:00 grep postfix
#安裝httpd服務器
[root@client01 ~]# yum install httpd -y
[root@client01 ~]# /etc/init.d/httpd start
Starting httpd: httpd:apr_sockaddr_info_get() failed for client01.host.com
httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@client01 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
[root@client01 ~]# /etc/init.d/httpd start
Starting httpd: httpd:apr_sockaddr_info_get() failed for client01.host.com
httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@client01 ~]# ps -ef|grep httpd
root 2203 1 0 13:35 ? 00:00:00 /usr/sbin/httpd
apache 2205 2203 0 13:35 ? 00:00:00 /usr/sbin/httpd
apache 2206 2203 0 13:35 ? 00:00:00 /usr/sbin/httpd
apache 2207 2203 0 13:35 ? 00:00:00 /usr/sbin/httpd
apache 2208 2203 0 13:35 ? 00:00:00 /usr/sbin/httpd
apache 2209 2203 0 13:35 ? 00:00:00 /usr/sbin/httpd
apache 2210 2203 0 13:35 ? 00:00:00 /usr/sbin/httpd
apache 2211 2203 0 13:35 ? 00:00:00 /usr/sbin/httpd
apache 2212 2203 0 13:35 ? 00:00:00 /usr/sbin/httpd
root 2214 1157 0 13:35 pts/0 00:00:00 grep httpd
[root@client01 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
[root@client01 ~]# ps -ef|grep httpd
root 2222 1157 0 13:37 pts/0 00:00:00 grep httpd
#關閉一個進程
#一個服務:多個進程組成
#模擬孤兒進程
#kill 殺死進程
[root@client01 ~]# /etc/init.d/httpd start
Starting httpd:
[root@client01 ~]# ps -ef|grep httpd
root 2228 1 0 13:38 ? 00:00:00 /usr/sbin/httpd
apache 2230 2228 0 13:38 ? 00:00:00 /usr/sbin/httpd
apache 2231 2228 0 13:38 ? 00:00:00 /usr/sbin/httpd
apache 2232 2228 0 13:38 ? 00:00:00 /usr/sbin/httpd
apache 2233 2228 0 13:38 ? 00:00:00 /usr/sbin/httpd
apache 2234 2228 0 13:38 ? 00:00:00 /usr/sbin/httpd
apache 2235 2228 0 13:38 ? 00:00:00 /usr/sbin/httpd
apache 2236 2228 0 13:38 ? 00:00:00 /usr/sbin/httpd
apache 2237 2228 0 13:38 ? 00:00:00 /usr/sbin/httpd
root 2250 1157 0 13:40 pts/0 00:00:00 grep httpd
#殺掉後,再次查詢,沒有該服務
[root@client01 ~]# kill 2228
[root@client01 ~]# ps -ef|grep httpd
root 2252 1157 0 13:40 pts/0 00:00:00 grep httpd
#模擬孤兒進程
[root@client01 ~]# /etc/init.d/httpd start
Starting httpd: httpd:apr_sockaddr_info_get() failed for client01.host.com
httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@client01 ~]# ps -ef|grep httpd
root 2258 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2260 2258 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2261 2258 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2262 2258 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2263 2258 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2264 2258 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2265 2258 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2266 2258 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2267 2258 0 13:41 ? 00:00:00 /usr/sbin/httpd
root 2269 1157 0 13:41 pts/0 00:00:00 grep httpd
#強制殺死
[root@client01 ~]# kill -9 2258
[root@client01 ~]# ps -ef|grep httpd
apache 2260 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2261 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2262 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2263 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2264 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2265 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2266 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2267 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
root 2271 1157 0 13:41 pts/0 00:00:00 grep httpd
#訪問:可以正常浏覽器訪問,說明服務正常,但是以後是否能訪問就不一定,這些進程過一段會自動回收
[root@larrywen ule-sa1]# firefox
http://192.168.1.11/
[root@client01 ~]# /etc/init.d/httpdrestart
Stopping httpd: [FAILED]
Starting httpd: httpd:apr_sockaddr_info_get() failed for client01.host.com
httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName
(98)Address already in use: make_sock:could not bind to address [::]:80
(98)Address already in use: make_sock:could not bind to address 0.0.0.0:80
no listening sockets available, shuttingdown
Unable to open logs
[FAILED]
[root@client01 ~]# ps -ef|grep httpd
apache 2260 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2261 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2262 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2263 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2264 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2265 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2266 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2267 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
root 2326 1157 0 13:45 pts/0 00:00:00 grep httpd
[root@client01 ~]# kill 2260
[root@client01 ~]# ps -ef|grep httpd
apache 2261 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2262 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2263 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2264 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2265 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2266 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
apache 2267 1 0 13:41 ? 00:00:00 /usr/sbin/httpd
root 2328 1157 0 13:45 pts/0 00:00:00 grep httpd
#正則匹配,全部殺死
[root@client01 ~]# pkill httpd
[root@client01 ~]# ps -ef|grep httpd
root 2331 1157 0 13:45 pts/0 00:00:00 grep httpd
[root@client01 ~]# /etc/init.d/httpd start
Starting httpd: httpd:apr_sockaddr_info_get() failed for client01.host.com
httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@client01 ~]# ps -ef|grep httpd
root 2337 1 0 13:47 ? 00:00:00 /usr/sbin/httpd
apache 2339 2337 0 13:47 ? 00:00:00 /usr/sbin/httpd
apache 2340 2337 0 13:47 ? 00:00:00 /usr/sbin/httpd
apache 2341 2337 0 13:47 ? 00:00:00 /usr/sbin/httpd
apache 2342 2337 0 13:47 ? 00:00:00 /usr/sbin/httpd
apache 2343 2337 0 13:47 ? 00:00:00 /usr/sbin/httpd
apache 2344 2337 0 13:47 ? 00:00:00 /usr/sbin/httpd
apache 2345 2337 0 13:47 ? 00:00:00 /usr/sbin/httpd
apache 2346 2337 0 13:47 ? 00:00:00 /usr/sbin/httpd
root 2348 1157 0 13:48 pts/0 00:00:00 grep httpd
#要查清楚,強制殺:很危險
[root@client01 ~]# pkill -9 httpd
[root@client01 ~]# ps -ef|grep httpd
root 2351 1157 0 13:48 pts/0 00:00:00 grep httpd
#列出所有的信號量
[root@client01 ~]# kill -l
1)SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6)SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12)SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22)SIGTTOU 23) SIGURG 24) SIGXCPU 25)SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30)SIGPWR
31) SIGSYS 34)SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37)SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42)SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45)SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50)SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55)SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62)SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
#使用killall命令(啟動的服務名字,不能通配使用)。建議使用,避免誤殺(因為其他服務有可能重名)
[root@client01 ~]# /etc/init.d/httpd start
Starting httpd: httpd:apr_sockaddr_info_get() failed for client01.host.com
httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@client01 ~]# ps -ef|grep http
root 2366 1 0 13:52 ? 00:00:00 /usr/sbin/httpd
apache 2368 2366 0 13:52 ? 00:00:00 /usr/sbin/httpd
apache 2369 2366 0 13:52 ? 00:00:00 /usr/sbin/httpd
apache 2370 2366 0 13:52 ? 00:00:00 /usr/sbin/httpd
apache 2371 2366 0 13:52 ? 00:00:00 /usr/sbin/httpd
apache 2372 2366 0 13:52 ? 00:00:00 /usr/sbin/httpd
apache 2373 2366 0 13:52 ? 00:00:00 /usr/sbin/httpd
apache 2374 2366 0 13:52 ? 00:00:00 /usr/sbin/httpd
apache 2375 2366 0 13:52 ? 00:00:00 /usr/sbin/httpd
root 2377 1157 0 13:53 pts/0 00:00:00 grep http
[root@client01 ~]# killall httpd
[root@client01 ~]# ps -ef|grep http
root 2380 1157 0 13:53 pts/0 00:00:00 grep http
[root@client01 ~]# /etc/init.d/httpd start
Starting httpd: httpd:apr_sockaddr_info_get() failed for client01.host.com
httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@client01 ~]##xkill命令
killall http
#xkill命令(點誰誰死)
[root@larrywen Desktop]# yum install xkill-y
[root@larrywen Desktop]# xkill
Select the window whose client you wish tokill with button 1....
#保存top的信息然後用於分析
[root@client01 ~]# top -b -n 2 > top.txt
四 前台進程和後台進程
[plain]
#前台進程
[root@client01 ~]# find / -name"aaaaa"
[root@client01 ~]# vim aa01.txt
#後台進程
[root@client01 ~]# vim aa01.txt
[1]+ Stopped vimaa01.txt
[root@client01 ~]# ps -ef|grep vim
root 2561 2426 0 14:09 pts/0 00:00:00 vim aa01.txt
root 2563 2426 0 14:09 pts/0 00:00:00 grep vim
[root@client01 ~]# vim aa02.txt
[2]+ Stopped vimaa02.txt
[root@client01 ~]# vim aa03.txt
[3]+ Stopped vimaa03.txt
#查看任務
[root@client01 ~]# jobs
[1] Stopped vimaa01.txt
[2]- Stopped vimaa02.txt
[3]+ Stopped vimaa03.txt
#按Ctrl+Z將進程停止,可以發現拷貝的文件大小不變
[root@larrywen opt]# cp -i/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso ./
^Z
[1]+ Stopped cp -i -i/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso ./
[root@larrywen opt]# ll rhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 163512320 Aug 5 14:13 rhel-server-6.1-x86_64-dvd.iso
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 163512320 Aug 5 14:13 rhel-server-6.1-x86_64-dvd.iso
[root@larrywen opt]# jobs
[1]+ Stopped cp -i -i/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso ./
#放到後台執行 bg加編號
[root@larrywen opt]# bg %1
[1]+ cp -i -i/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso ./ &
[root@larrywen opt]# ll rhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 487411712 Aug 5 14:13 rhel-server-6.1-x86_64-dvd.iso
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 683212800 Aug 5 14:13 rhel-server-6.1-x86_64-dvd.iso
[root@client01 ~]# bg %1
[1] vim aa01.txt &
#放到前台執行
[root@client01 ~]# fg %1
vim aa01.txt
[root@client01 ~]# jobs
[2]- Stopped vimaa02.txt
[3]+ Stopped vimaa03.txt
#放到後台執行
[root@larrywen opt]# cp /home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso./ &
[1] 12611
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 1289060352 Aug 5 14:16 rhel-server-6.1-x86_64-dvd.iso
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 1392214016 Aug 5 14:16 rhel-server-6.1-x86_64-dvd.iso
[1]+ Done cp -i/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso ./
[root@larrywen opt]# cp/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso ./ &
[1] 12647
[root@larrywen opt]# jobs
[1]+ Running cp -i/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso ./ &
#Ctrl+C:中斷,可以發現文件大小不變
[root@larrywen opt]# fg %1
cp -i/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso ./
^C
[root@larrywen opt]# jobs
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 2298707968 Aug 5 14:17 rhel-server-6.1-x86_64-dvd.iso
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 2298707968 Aug 5 14:17 rhel-server-6.1-x86_64-dvd.iso
[root@client01 ~]# jobs
[2]- Stopped vimaa02.txt
[3]+ Stopped vimaa03.txt
[root@client01 ~]# kill %2
[2]- Stopped vimaa02.txt
[root@client01 ~]# jobs
[2]+ Stopped vim aa02.txt
[3]- Stopped vimaa03.txt
[root@client01 ~]# kill -9 %2
[2]+ Stopped vimaa02.txt
[root@client01 ~]# jobs
[2]+ Killed vimaa02.txt
[3]- Stopped vimaa03.txt
[root@client01 ~]# jobs
[3]+ Stopped vimaa03.txt
[root@client01 ~]# kill -9 %3
[3]+ Stopped vimaa03.txt
[root@client01 ~]# jobs
[3]+ Killed vimaa03.txt
[root@client01 ~]# jobs
#後台執行
[root@client01 ~]# cp rhel.iso /opt/ &
[root@client01 ~]# nohup cp rhel.iso /opt/
#拷貝的時候關閉終端
[root@larrywen Desktop]# scp/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso 192.168.1.11:/root
#文件大小不變
[root@client01 ~]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 171425792 Aug 5 14:33 rhel-server-6.1-x86_64-dvd.iso
[root@client01 ~]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 171425792 Aug 5 14:33 rhel-server-6.1-x86_64-dvd.iso
[root@client01 ~]# llrhel-server-6.1-x86_64-dvd.iso
[root@client01 ~]# rm -frhel-server-6.1-x86_64-dvd.iso
#後台執行,&,關閉終端後仍然在拷貝
[root@larrywen opt]# cp/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso /opt &
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 3600812032 Aug 5 14:38 rhel-server-6.1-x86_64-dvd.iso
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 3600812032 Aug 5 14:38 rhel-server-6.1-x86_64-dvd.iso
#後台執行,nohup,關閉終端後仍然在拷貝
[root@larrywen opt]# nohup cp/home/Wentasy/software/rhel-server-6.1-x86_64-dvd.iso /opt
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 2561306624 Aug 5 14:40 rhel-server-6.1-x86_64-dvd.iso
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso
-rwxr-xr-x. 1 root root 2665922560 Aug 5 14:40 rhel-server-6.1-x86_64-dvd.iso
[root@larrywen opt]# llrhel-server-6.1-x86_64-dvd.iso