Linux Command - ps, kill
linux 下的命令 ps, kill 简介。
ps#
以下为为各大平台 ps 命令在线手册:
- unix/POSIX - ps - report process status
- FreeBSD/Darwin - ps -- process status
- linux - ps - report a snapshot of the current processes
- debian - ps - report a snapshot of the current processes
- ubuntu - ps - report a snapshot of the current processes
- windows - tasklist - Displays a list of currently running processes
$ man ps # macOS
PS(1) BSD General Commands Manual PS(1)
NAME
ps -- process status
SYNOPSIS
ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]] [-g grp[,grp...]] [-u uid[,uid...]]
[-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]
ps [-L]
ps
只列举当前用户进程,ps -A
或 ps -e
则会列举其他用户进程。
$ ps
PID TTY TIME CMD
1701 ttys000 0:00.10 /bin/zsh -l
4338 ttys004 0:03.58 /Applications/Xcode.app/Contents/Developer/usr/bin/xcdevice observe --both
13387 ttys005 0:01.47 -zsh
9710 ttys007 0:00.29 ssh [email protected]
$ ps x
PID TT STAT TIME COMMAND
1701 s000 Ss+ 0:00.10 /bin/zsh -l
4338 s004 Ss+ 0:03.58 /Applications/Xcode.app/Contents/Developer/usr/bin/xcdevice observe --both
13386 s005 Ss 0:00.06 /usr/bin/login -fpl faner /Applications/iTerm.app/Contents/MacOS/ShellLauncher --launch_shell
13387 s005 S 0:01.57 -zsh
16312 s005 R+ 0:00.00 ps x
9710 s007 Ss+ 0:00.29 ssh [email protected]
pstree#
$ man pstree # on macOS
PSTREE(1) BSD General Commands Manual PSTREE(1)
NAME
pstree -- list processes as a tree
SYNOPSIS
pstree [-Uw] [-f file] [-g n] [-l n] [-p pid] [-s string] [-u user] [rootpid ...]
top#
$ man top # macOS
TOP(1) BSD General Commands Manual TOP(1)
NAME
top -- display sorted information about processes
SYNOPSIS
top [-a | -d | -e | -c mode]
[-F | -f]
[-h]
[-i interval]
[-l samples]
[-ncols columns]
[-o key | -O skey]
[-R | -r]
[-S]
[-s delay-secs]
[-n nprocs]
[-stats keys]
[-pid processid]
[-user username]
[-U username]
[-u]
kill#
$ man kill # macOS
KILL(1) BSD General Commands Manual KILL(1)
NAME
kill -- terminate or signal a process
SYNOPSIS
kill [-s signal_name] pid ...
kill -l [exit_status]
kill -signal_name pid ...
kill -signal_number pid ...
pgrep/pkill#
$ man pkill # macOS
PKILL(1) BSD General Commands Manual PKILL(1)
NAME
pgrep, pkill -- find or signal processes by name
SYNOPSIS
pgrep [-Lafilnoqvx] [-F pidfile] [-G gid] [-P ppid] [-U uid] [-d delim] [-g pgrp] [-t tty] [-u euid] pattern ...
pkill [-signal] [-ILafilnovx] [-F pidfile] [-G gid] [-P ppid] [-U uid] [-g pgrp] [-t tty] [-u euid] pattern ...
Bash: Killing all processes in subprocess
You can set a trap in the subshell to kill any active jobs before exiting:
This does not work for me. What is jobs -p supposed to output? It doesn't output anything when I run it.
I don't know exactly why that sleep process gets orphaned, anyway instead kill you can use pkill with -P flag to also kill all children
EDIT: that means that in order to kill a process and all it's children you should use instead
# gets pids of child processes
CPIDS=`pgrep -P $pid`
kill -KILL $pid
for cpid in $CPIDS ; do kill -9 $cpid ; done
pslist/rkill#
PSLIST(1) BSD General Commands Manual PSLIST(1)
NAME
pslist -- control processes and their descendants
SYNOPSIS
pslist [pid/name...]
pslist -h | --help
pslist -v | --version
rkill [-SIG] pid/name...
rrenice [+/-]pri pid/name...
Bash: Killing all processes in subprocess
You can have a look at rkill
that seems to meet your requirements :
Works, though
pslist
/rkill
does not seem to be standard tool installed on most Linux systems
systemadmin#
ohmyzsh/plugins/systemadmin/ 提供了一些 ps 和 kill 的便捷封装。
pkill 杀死指定process name,kill则基于PID杀进程。
# 杀死挂起进程
kill_run() {
pkill -KILL nginx # default SIGTERM
pgrep -f 'dart run.*SHELF_PROXY' | xargs kill -KILL
pgrep -f 'flutter run.*NGINX_REVERSE_PROXY' | xargs kill -KILL
# ps -ef | grep '(dart|flutter) run' | grep -v grep | awk '{ print $2}' | xargs kill -KILL
}
查看 Linux TCP Port 被哪隻程式(Process)佔用
Finding the PID of the Process Using a Specific Port
Linux Find Out Which Process Is Listening Upon a Port
3 Ways to Find Out Which Process Listening on a Particular Port