大部分表述完全从转载处复制而来,其中命令部分倒是在我那配置超低的虚拟机上操作。
【常规篇】
ps -ef
root@extmail ~$ps -ef | tail
root 2891 1 0 May29 tty6 00:00:00 /sbin/mingetty tty6
root 2939 1 0 May29 ? 00:02:44 /usr/bin/python -tt /usr/sbin/yum-updatesd
root 2941 1 0 May29 ? 00:00:00 /usr/libexec/gam_server
root 2944 2883 0 May29 tty1 00:00:00 -bash
root 7987 2712 0 20:22 ? 00:00:00 sshd: root@pts/0
root 7989 7987 0 20:22 pts/0 00:00:00 -bash
root 8625 1 0 21:00 ? 00:00:00 syslogd -m 0
root 8628 1 0 21:00 ? 00:00:00 klogd -x
root 8654 7989 0 21:05 pts/0 00:00:00 ps -ef
root 8655 7989 0 21:05 pts/0 00:00:00 tail
【附加】
可对进程名进行排序,使其更具有可观性,便于查找。(即针对第八列排序即可)
root@extmail ~$ps -ef | tail | sort -k 8
root 7989 7987 0 20:22 pts/0 00:00:00 -bash
root 8908 8906 0 21:33 pts/2 00:00:00 -bash
root 9283 1 0 21:45 ? 00:00:00 crond
root 9566 1 0 22:07 ? 00:00:00 klogd -x
root 9570 8908 0 22:07 pts/2 00:00:00 ps -ef
root 9572 8908 0 22:07 pts/2 00:00:00 sort -k 8
root 7987 2712 0 20:22 ? 00:00:00 sshd: root@pts/0
root 8906 2712 0 21:33 ? 00:00:00 sshd: root@pts/2
root 9563 1 0 22:07 ? 00:00:00 syslogd -m 0
root 9571 8908 0 22:07 pts/2 00:00:00 tail
ps aux
root@extmail ~$ps aux | tail
root 2891 0.0 0.0 3788 524 tty6 Ss+ May29 0:00 /sbin/mingetty tty6
root 2939 0.0 1.9 260924 19828 ? SN May29 2:44 /usr/bin/python -tt /usr/sbin/yum-updatesd
root 2941 0.0 0.1 12912 1224 ? SN May29 0:00 /usr/libexec/gam_server
root 2944 0.0 0.1 66068 1624 tty1 Ss+ May29 0:00 -bash
root 7987 0.0 0.3 90900 3316 ? Ss 20:22 0:00 sshd: root@pts/0
root 7989 0.0 0.1 66232 1724 pts/0 Ss 20:22 0:00 -bash
root 8625 0.0 0.0 5908 656 ? Ss 21:00 0:00 syslogd -m 0
root 8628 0.0 0.0 3804 420 ? Ss 21:00 0:00 klogd -x
root 8662 0.0 0.0 65628 956 pts/0 R+ 21:07 0:00 ps aux
root 8663 0.0 0.0 58948 476 pts/0 R+ 21:07 0:00 tail
若想杀死syslog进程,执行kill -s 9 8625即可
其中-s 9 制定了传递给进程的信号是9,即强制、尽快终止进程。
但有个问题,进程少了则无所谓,进程多了,就会觉得痛苦了,无论是ps -ef 还是ps -aux,每次都要在一大串进程信息里面查找到要杀的进程,看的眼都花了。
【进阶篇】
把ps的查询结果通过管道给grep查找包含特定字符串的进程。管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。
root@extmail ~$ps -ef | grep syslog
root 8692 1 0 21:12 ? 00:00:00 syslogd -m 0
root 8698 7989 0 21:12 pts/0 00:00:00 grep syslog
这次就清爽了。然后就是
root@extmail ~$kill -s 9 8692
使用pgrep与kill -s 9
先使用pgrep查出进程syslogd对应的PID,然后用kill -s 9 PID结束掉该进程。
root@extmail ~$pgrep syslog
8719
root@extmail ~$kill -s 9 8719
使用pgrep时无需输入进程的全名也可查找出对应的PID,如查找syslogd的PID,只需输入pgrep syslo即可
root@extmail ~$pgrep syslogd
8719
root@extmail ~$pgrep syslo
8719
grep与xargs
$ps -ef | grep syslog | grep -v grep | cut -c 11-15 | xargs kill -s 9
说明:
“grep firefox”的输出结果是,所有含有关键字“firefox”的进程。
“grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。
“cut -c 11-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。
“xargs kill -s 9”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -s 9”命令的参数,并执行该命令。“kill -s 9”会强行杀掉指定进程。
难道你不想抱怨点什么?没错太长了
pgrep与xargs
root@extmail ~$pgrep syslog
9346
root@extmail ~$pgrep syslog | xargs kill -s 9
root@extmail ~$pgrep syslog
ps -ef | grep firefox | awk '{print $2}' | xargs kill -9
kill -s 9与反引号结合,awk,grep,grep -v
难道每次都要调用xargs把PID传递给kill?答案是否定的:
来看看我大反引号的魅力吧
$kill -s 9 `ps -ef | grep syslog | grep -v grep |awk '{print $2}'`
$kill -s 9 $(ps -ef | grep syslog | grep -v grep |awk '{print $2}')
没错,命令依然有点长,换成pgrep。
$kill -s 9 `pgrep syslog`
看到pkill想到了什么?没错pgrep和kill!pkill=pgrep+kill。
root@extmail ~$pgrep syslog
9900
root@extmail ~$pkill -9 syslog
root@extmail ~$
root@extmail ~$pgrep syslog
说明:"-9" 即发送的信号是9,pkill与kill在这点的差别是:pkill无须 “s”,终止信号等级直接跟在 “-“ 后面。之前我一直以为是 "-s 9",结果每次运行都无法终止进程。
killall -9
killall和pkill是相似的,不过如果给出的进程名不完整,killall会报错。pkill或者pgrep只要给出进程名的一部分就可以终止进程。
root@extmail ~$killall sysl
sysl: no process killed
root@extmail ~$killall -9 syslog
【总结】
查找进程号PID
ps -ef | grep [s]yslog | awk '{print $2}'
pgrep syslo
pidof syslogd(必须给出进程的全名)
杀死进程
根据PID(进程号)
kill -s 9 PID
根据进程名
pkill syslo (pgrep与kill) 其后跟进程名,可不写全名,注意无-s
killall syslogd (必须给出进程的全名)