- 本系列文章,将整理一系列Linux环境下进程相关的操作命令,包括进程启动、kill、挂起、查看、前后台进程切换等各种命令。
- 本文为本系列的第三篇,进程的kill,主要关注如何kill进程的相关操作。
本文涉及的kill命令,都依赖当前系统的进程情况,本文仅以make这个进程为例来说明:
[qxhgd@localhost]$ ps -a PID TTY TIME CMD 708 pts/25 00:00:00 make
kill
- kill命令man手册的说明
NAME kill - terminate a process SYNOPSIS kill [-s signal|-p] [-q sigval] [-a] [--] pid... kill -l [signal] DESCRIPTION The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught. Most modern shells have a builtin kill function, with a usage rather similar to that of the command described here. The '-a' and '-p' options, and the possibility to specify processes by command name are a local extension. If sig is 0, then no signal is sent, but error checking is still performed.
- 主要有两种操作:
– 查看不同信号ID的含义;
– 给进程发信号,进程以PID代表;
- kill操作实例:
– 发信号:
[qxhgd@localhost]$kill 708 #SIGTERM,终止进程, [qxhgd@localhost]$kill -19 708 #SIGSTOP,暂停进程; [qxhgd@localhost]$kill -18 708 #SIGSTOP,暂停进程; [qxhgd@localhost]$kill -9 708 #SIGKILL,杀掉进程;
– 查看各信号含义:
[qxhgd@localhost]$ 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
- man手册说明:
NAME killall - kill processes by name SYNOPSIS killall [-Z, --context pattern] [-e, --exact] [-g, --process-group] [-i, --interactive] [-o, --older-than TIME] [-q, --quiet] [-r, --regexp] [-s, --signal signal] [-u, --user user] [-v, --verbose] [-w, --wait] [-y, --younger-than TIME] [-I, --ignore-case] [-V, --version] [--] name ... killall -l killall -V, --version DESCRIPTION killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent. Signals can be specified either by name (e.g. -HUP or -SIGHUP) or by number (e.g. -1) or by option -s. If the command name is not regular expression (option -r) and contains a slash (/), processes executing that particular file will be selected for killing, independent of their name. killall returns a zero return code if at least one process has been killed for each listed command, or no commands were listed and at least one process matched the -u and -Z search criteria. kil‐ lall returns non-zero otherwise. A killall process never kills itself (but may kill other killall processes).
- 和kill不用的是,killall的参数是进程名,根据进程名对进程进行操作;
[qxhgd@localhost]$killall make #杀掉make [qxhgd@localhost]$killall -19 make #暂停make
pkill
- man手册说明
NAME pgrep, pkill - look up or signal processes based on name and other attributes SYNOPSIS pgrep [options] pattern pkill [options] pattern DESCRIPTION pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout. All the criteria have to match. For example, $ pgrep -u root sshd will only list the processes called sshd AND owned by root. On the other hand, $ pgrep -u root,daemon will list the processes owned by root OR daemon. pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout.
- 命令可以按照进程名杀死进程,和 killall 差不多,也是批量杀死运行中的程序;
ctrl命令
- 前台进程的终止,ctrl+C
- 前台进程的挂起,ctrl+Z
- 对后台进程无能为力;
GUI
- 对GUI程序,关闭窗口即关闭进程,和windows类似。