关于进程管理,想必有些网友都有所耳闻,但是并不了解。所以呢,为了让网友们能够了解进程管理的相关知识,本次小编就和大家一起来了解这些个知识吧!
一 进程相关概念
进程:数据处理,管理数据,对数据进行计算。独占资源,数据处理,一个进程可以有多个线程,也可以没有;
线程:依附于进程运行,也是处理数据。线程之间共享资源;一个线程中不能有进程,而且线程中不允许有线程;可以启动多个线程,但每次执行的结果不一样
二 查看进程相关命令
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
[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