linux 命令使用1--ps
1. 参数意义
- a:
所有进程
Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal.
- u:
显示所有包含其他使用者的进程
- x:
通常与a 这个参数一起使用,可列出较完整信息。
- e:
所有进程
Select all processes. Identical to -A.
- f:
完整格式列出
Do full-format listing. This option can be combined with many other UNIX-style options to add additional columns. It also causes the command arguments to be printed. When
used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added. See the c option, the format keyword args, and the format keyword comm.
2. 常用
2.1. ps aux—Unix Style
zhancj@iZbp10j40ovkbwx9an9ppuZ:~$ ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 227224 7620 ? Ss 2020 38:56 /sbin/init noibrs splash root 2 0.0 0.0 0 0 ? S 2020 0:12 [kthreadd] root 4 0.0 0.0 0 0 ? I< 2020 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? I< 2020 0:00 [mm_percpu_wq] root 7 0.0 0.0 0 0 ? S 2020 60:22 [ksoftirqd/0] root 8 0.0 0.0 0 0 ? I 2020 660:40 [rcu_sched] root 9 0.0 0.0 0 0 ? I 2020 0:00 [rcu_bh]
显示列名:
USER //用户名 %CPU //进程占用的CPU百分比 %MEM //占用内存的百分比 VSZ //该进程使用的虚拟內存量(KB) RSS //该进程占用的固定內存量(KB)(驻留中页的数量) STAT //进程的状态 START //该进程被触发启动时间 TIME //该进程实际使用CPU运行的时间
2.2. ps -ef—System V Style
zhancj@iZbp10j40ovkbwx9an9ppuZ:~$ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 2020 ? 00:38:56 /sbin/init noibrs splash root 2 0 0 2020 ? 00:00:12 [kthreadd] root 4 2 0 2020 ? 00:00:00 [kworker/0:0H] root 6 2 0 2020 ? 00:00:00 [mm_percpu_wq] root 7 2 0 2020 ? 01:00:22 [ksoftirqd/0] root 8 2 0 2020 ? 11:00:40 [rcu_sched] root 9 2 0 2020 ? 00:00:00 [rcu_bh]
显示列名:
UID //用户ID、但输出的是用户名 PID //进程的ID PPID //父进程ID C //进程占用CPU的百分比 STIME //进程启动到现在的时间 TTY //该进程在那个终端上运行,若与终端无关,则显示? 若为pts/0等,则表示由网络连接主机进程。 CMD //命令的名称和参数
3. 命令内排序
根据列名排序,如果列是以%开头,加p;否则直接使用字段
3.1. 按照cpu降序排序:
ps -aux —sort -pcpu
zhancj@iZbp10j40ovkbwx9an9ppuZ:~$ ps -aux --sort -pcpu|head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 13338 6.0 1.1 2640268 92228 ? Ssl 2021 20668:37 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --cgroup-driver=cgroupfs --hostname-override=iz2zeabl8ta0jq1nd850igz --network-plugin=cni --pod-infra-container-image=k8s.gcr.io/pause:3.2 --resolv-conf=/run/systemd/resolve/resolv.conf root 14082 3.4 4.1 573712 337000 ? Ssl 2021 11680:34 kube-apiserver --advertise-address=172.16.207.15 --allow-privileged=true --authorization-mode=Node,RBAC --client-ca-file=/etc/kubernetes/pki/ca.crt --enable-admission-plugins=NodeRestriction --enable-bootstrap-token-auth=true --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key --etcd-servers=https://127.0.0.1:2379 --insecure-port=0 --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key --requestheader-allowed-names=front-proxy-client --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --secure-port=6443 --service-account-key-file=/etc/kubernetes/pki/sa.pub --service-cluster-ip-range=10.96.0.0/12 --service-node-port-range=1-65535 --tls-cert-file=/etc/kubernetes/pki/apiserver.crt --tls-private-key-file=/etc/kubernetes/pki/apiserver.key root 19940 2.2 0.2 279352 23588 ? S<sl Aug15 99:31 /usr/local/aegis/aegis_client/aegis_11_35/AliYunDun systemd+ 10798 1.6 6.3 1298728 519196 ? Ssl 2021 5770:18 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.7 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id
3.2. 按照内存降序排序:
ps -aux —sort -pmem
zhancj@iZbp10j40ovkbwx9an9ppuZ:~$ ps -aux --sort -pmem|head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND systemd+ 10973 1.3 6.4 1298920 529780 ? Ssl 2021 4623:29 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.6 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id root 25312 0.0 6.4 1019320 526468 ? Sl Aug17 0:45 gunicorn: worker [mage_omp.wsgi:application] systemd+ 10798 1.6 6.3 1298728 519196 ? Ssl 2021 5770:20 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.7 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id systemd+ 7269 1.3 6.1 1230756 501804 ? Ssl 2021 4592:23 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.4 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id
3.3. 按照缓存降序排序:
ps -aux --sort -rss
zhancj@iZbp10j40ovkbwx9an9ppuZ:~$ ps -aux --sort -rss|head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND systemd+ 10973 1.3 6.4 1298920 529780 ? Ssl 2021 4623:30 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.6 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id root 25312 0.0 6.4 1019320 526468 ? Sl Aug17 0:45 gunicorn: worker [mage_omp.wsgi:application] systemd+ 10798 1.6 6.3 1298728 519196 ? Ssl 2021 5770:20 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.7 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id systemd+ 7269 1.3 6.1 1230756 501804 ? Ssl 2021 4592:23 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.4 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id
4. 结合sort命令排序
sort -rn -k3
r表示reverse反向排序
n表示以整数进行排序
k表示以第几项进行排序
后面在sort命令单独拿出来列一个,这里
4.1. 按照cpu降序排序:
ps aux|head -1;ps auxw|sort -rn -k3
zhancj@iZbp10j40ovkbwx9an9ppuZ:~$ ps aux|head -1;ps auxw|sort -rn -k3 |head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 13338 6.0 1.1 2640268 92228 ? Ssl 2021 20668:50 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --cgroup-driver=cgroupfs --hostname-override=iz2zeabl8ta0jq1nd850igz --network-plugin=cni --pod-infra-container-image=k8s.gcr.io/pause:3.2 --resolv-conf=/run/systemd/resolve/resolv.conf
root 14082 3.4 4.1 573712 337000 ? Ssl 2021 11680:40 kube-apiserver --advertise-address=172.16.207.15 --allow-privileged=true --authorization-mode=Node,RBAC --client-ca-file=/etc/kubernetes/pki/ca.crt --enable-admission-plugins=NodeRestriction --enable-bootstrap-token-auth=true --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key --etcd-servers=https://127.0.0.1:2379 --insecure-port=0 --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key --requestheader-allowed-names=front-proxy-client --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --secure-port=6443 --service-account-key-file=/etc/kubernetes/pki/sa.pub --service-cluster-ip-range=10.96.0.0/12 --service-node-port-range=1-65535 --tls-cert-file=/etc/kubernetes/pki/apiserver.crt --tls-private-key-file=/etc/kubernetes/pki/apiserver.key
root 19940 2.2 0.2 279352 23588 ? S<sl Aug15 99:35 /usr/local/aegis/aegis_client/aegis_11_35/AliYunDun
systemd+ 10798 1.6 6.3 1298728 519196 ? Ssl 2021 5770:22 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.7 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id
root 14284 1.6 0.8 215000 66268 ? Ssl Jul05 1016:49 kube-controller-manager --allocate-node-cidrs=true --authentication-kubeconfig=/etc/kubernetes/controller-manager.conf --authorization-kubeconfig=/etc/kubernetes/controller-manager.conf --bind-address=127.0.0.1 --client-ca-file=/etc/kubernetes/pki/ca.crt --cluster-cidr=10.244.0.0/16 --cluster-name=kubernetes --cluster-signing-cert-file=/etc/kubernetes/pki/ca.crt --cluster-signing-key-file=/etc/kubernetes/pki/ca.key --controllers=*,bootstrapsigner,tokencleaner --kubeconfig=/etc/kubernetes/controller-manager.conf --leader-elect=true --node-cidr-mask-size=24 --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt --root-ca-file=/etc/kubernetes/pki/ca.crt --service-account-private-key-file=/etc/kubernetes/pki/sa.key --service-cluster-ip-range=10.96.0.0/12 --use-service-account-credentials=true
这句话是为了输出第一行的字段意义:ps aux|head -1;
4.2. 按照内存百分比降序排序:
ps auxw|head -1;ps auxw|sort -rn -k4
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND systemd+ 10973 1.3 6.4 1298920 529780 ? Ssl 2021 4623:36 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.6 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id root 25312 0.0 6.4 1019320 526456 ? Sl Aug17 0:45 gunicorn: worker [mage_omp.wsgi:application] systemd+ 10798 1.6 6.3 1298728 519196 ? Ssl 2021 5770:24 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.7 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id systemd+ 7269 1.3 6.1 1230756 501804 ? Ssl 2021 4592:27 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.4 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id root 14082 3.4 4.1 573712 337000 ? Ssl 2021 11680:45 kube-apiserver --advertise-address=172.16.207.15 --allow-privileged=true --authorization-mode=Node,RBAC --client-ca-file=/etc/kubernetes/pki/ca.crt --enable-admission-plugins=NodeRestriction --enable-bootstrap-token-auth=true --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key --etcd-servers=https://127.0.0.1:2379 --insecure-port=0 --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key --requestheader-allowed-names=front-proxy-client --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --secure-port=6443 --service-account-key-file=/etc/kubernetes/pki/sa.pub --service-cluster-ip-range=10.96.0.0/12 --service-node-port-range=1-65535 --tls-cert-file=/etc/kubernetes/pki/apiserver.crt --tls-private-key-file=/etc/kubernetes/pki/apiserver.key
4.3. 按照内存量降序排序:
ps aux|head -1;ps auxw|sort -rn -k6
zhancj@iZbp10j40ovkbwx9an9ppuZ:~$ ps aux|head -1;ps auxw|sort -rn -k6 |head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND systemd+ 10973 1.3 6.4 1298920 529780 ? Ssl 2021 4623:37 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.6 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id root 25312 0.0 6.4 1019320 526456 ? Sl Aug17 0:46 gunicorn: worker [mage_omp.wsgi:application] systemd+ 10798 1.6 6.3 1298728 519196 ? Ssl 2021 5770:24 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.7 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id systemd+ 7269 1.3 6.1 1230756 501804 ? Ssl 2021 4592:27 consul agent -data-dir=/consul/data -config-dir=/consul/config -server -bootstrap-expect=3 -ui -data-dir=/consul/data -bind=0.0.0.0 -client=0.0.0.0 -advertise=10.244.0.4 -retry-join=consul-server-0.consul-server.default.svc.cluster.local -retry-join=consul-server-1.consul-server.default.svc.cluster.local -retry-join=consul-server-2.consul-server.default.svc.cluster.local -domain=consul -disable-host-node-id root 14082 3.4 4.1 573712 337000 ? Ssl 2021 11680:46 kube-apiserver --advertise-address=172.16.207.15 --allow-privileged=true --authorization-mode=Node,RBAC --client-ca-file=/etc/kubernetes/pki/ca.crt --enable-admission-plugins=NodeRestriction --enable-bootstrap-token-auth=true --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key --etcd-servers=https://127.0.0.1:2379 --insecure-port=0 --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key --requestheader-allowed-names=front-proxy-client --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --secure-port=6443 --service-account-key-file=/etc/kubernetes/pki/sa.pub --service-cluster-ip-range=10.96.0.0/12 --service-node-port-range=1-65535 --tls-cert-file=/etc/kubernetes/pki/apiserver.crt --tls-private-key-file=/etc/kubernetes/pki/apiserver.key