Example of a method for Linux to view all information about a process
- 2020-12-16 06:18:15
- OfStack
There is a task process on the server, when we use ps-ef | grep task to view, we can only get 1 of the most basic information, if we need to know the corresponding file path, the full path of the boot command, there is no way to start.
Today I'm going to show you a way to see all the information about the process, including the full path of the startup command, the full path of the corresponding file, and so on: ES7en-ES8en /proc/PID
For example, when you view a process using ps-ef | grep webhook, you only get the following information
> ps -ef | grep webhook
root 15902 1 0 Aug15 ? 00:00:00 python -u src/webhook.py
root 30639 30612 0 22:50 pts/0 00:00:00 grep --color=auto webhook
Then execute ls-ES21en /proc/15902
> ll /proc/15902
dr-xr-xr-x 2 root root 0 Sep 18 22:52 attr
-rw-r--r-- 1 root root 0 Sep 18 22:52 autogroup
-r-------- 1 root root 0 Sep 18 22:52 auxv
-r--r--r-- 1 root root 0 Sep 18 22:52 cgroup
--w------- 1 root root 0 Sep 18 22:52 clear_refs
-r--r--r-- 1 root root 0 Aug 15 16:48 cmdline
-rw-r--r-- 1 root root 0 Sep 18 22:52 comm
-rw-r--r-- 1 root root 0 Sep 18 22:52 coredump_filter
-r--r--r-- 1 root root 0 Sep 18 22:52 cpuset
lrwxrwxrwx 1 root root 0 Aug 15 16:48 cwd -> /services/apps/webhook
-r-------- 1 root root 0 Sep 18 22:52 environ
lrwxrwxrwx 1 root root 0 Aug 15 16:48 exe -> /usr/bin/python2.7
dr-x------ 2 root root 0 Aug 15 16:48 fd
dr-x------ 2 root root 0 Sep 18 22:52 fdinfo
-rw-r--r-- 1 root root 0 Sep 18 22:52 gid_map
-r-------- 1 root root 0 Sep 18 22:52 io
-r--r--r-- 1 root root 0 Sep 18 22:52 limits
...
There are several major documents we can focus on:
cwd: The directory where the files are located exe: The full path to execute the command environ: Environment variable at command execution timeSupplement:
#lsof Lists the current system open file, often with -i Option to see which program is using a particular port
[root@bogon ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 6701 root 4u IPv6 64259 0t0 TCP *:http (LISTEN)
httpd 6702 apache 4u IPv6 64259 0t0 TCP *:http (LISTEN)
httpd 6703 apache 4u IPv6 64259 0t0 TCP *:http (LISTEN)
httpd 6704 apache 4u IPv6 64259 0t0 TCP *:http (LISTEN)
httpd 6705 apache 4u IPv6 64259 0t0 TCP *:http (LISTEN)
httpd 6706 apache 4u IPv6 64259 0t0 TCP *:http (LISTEN)
[root@bogon ~]# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 931 root 3u IPv4 20955 0t0 TCP *:ssh (LISTEN)
sshd 931 root 4u IPv6 20965 0t0 TCP *:ssh (LISTEN)
sshd 3926 root 3u IPv4 36374 0t0 TCP bogon:ssh->bogon:53436 (ESTABLISHED)
[root@bogon ~]#
# Gets all information about the process for all users on the terminal
[root@bogon ~]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 128092 4724 ? Ss Jun25 0:03 /usr/lib/systemd/syste
root 2 0.0 0.0 0 0 ? S Jun25 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Jun25 0:00 [ksoftirqd/0]
root 6 0.0 0.0 0 0 ? S Jun25 0:00 [kworker/u256:0]
root 7 0.0 0.0 0 0 ? S Jun25 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S Jun25 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? R Jun25 0:01 [rcu_sched]
root 10 0.0 0.0 0 0 ? S Jun25 0:00 [watchdog/0]
root 12 0.0 0.0 0 0 ? S< Jun25 0:00 [khelper]
root 13 0.0 0.0 0 0 ? S Jun25 0:00 [kdevtmpfs]
root 14 0.0 0.0 0 0 ? S< Jun25 0:00 [netns]
root 15 0.0 0.0 0 0 ? S Jun25 0:00 [khungtaskd]
root 16 0.0 0.0 0 0 ? S< Jun25 0:00 [writeback]
root 17 0.0 0.0 0 0 ? S< Jun25 0:00 [kintegrityd]
root 18 0.0 0.0 0 0 ? S< Jun25 0:00 [bioset]
root 19 0.0 0.0 0 0 ? S< Jun25 0:00 [kblockd]
root 20 0.0 0.0 0 0 ? S< Jun25 0:00 [md]
root 26 0.0 0.0 0 0 ? S Jun25 0:00 [kswapd0]
root 27 0.0 0.0 0 0 ? SN Jun25 0:00 [ksmd]
root 28 0.0 0.0 0 0 ? SN Jun25 0:00 [khugepaged]
root 29 0.0 0.0 0 0 ? S Jun25 0:00 [fsnotify_mark]
root 30 0.0 0.0 0 0 ? S< Jun25 0:00 [crypto]
root 38 0.0 0.0 0 0 ? S< Jun25 0:00 [kthrotld]
root 40 0.0 0.0 0 0 ? S< Jun25 0:00 [kmpath_rdacd]
root 41 0.0 0.0 0 0 ? S< Jun25 0:00 [kpsmoused]
root 43 0.0 0.0 0 0 ? S< Jun25 0:00 [ipv6_addrconf]
root 62 0.0 0.0 0 0 ? S< Jun25 0:00 [deferwq]
root 94 0.0 0.0 0 0 ? S Jun25 0:00 [kauditd]
root 233 0.0 0.0 0 0 ? S< Jun25 0:00 [mpt_poll_0]
root 234 0.0 0.0 0 0 ? S< Jun25 0:00 [ata_sff]
root 235 0.0 0.0 0 0 ? S< Jun25 0:00 [mpt/0]
root 241 0.0 0.0 0 0 ? S Jun25 0:00 [scsi_eh_0]
root 243 0.0 0.0 0 0 ? S< Jun25 0:00 [scsi_tmf_0]
root 245 0.0 0.0 0 0 ? S Jun25 0:00 [scsi_eh_1]
root 246 0.0 0.0 0 0 ? S Jun25 0:00 [kworker/u256:2]
.
# Gets all process numbers for a service program
[root@bogon ~]# pgrep httpd
6701
6702
6703
6704
6705
6706
# Kill the process, killall Kill all processes by program name
[root@bogon ~]# killall httpd
[root@bogon ~]# pgrep httpd
[root@bogon ~]#
#kill Kill the process
[root@bogon ~]# cat a.sh # Infinite loop
#!/bin/bash
while true
do
echo "linux">/dev/null
done
[root@bogon ~]# ./a.sh
^Z
[1]+ Stopped ./a.sh
[root@bogon ~]# ps
PID TTY TIME CMD
3930 pts/1 00:00:00 bash
11719 pts/1 00:00:01 a.sh
11729 pts/1 00:00:00 ps
[root@bogon ~]# kill 11719 # I found this problem by accident, the cause of which is temporarily unknown
[root@bogon ~]# ps
PID TTY TIME CMD
3930 pts/1 00:00:00 bash
11719 pts/1 00:00:01 a.sh
11733 pts/1 00:00:00 ps
[root@bogon ~]# kill -9 11719 # Since the ordinary kill Unable to kill, send -9 Signal direct force kill, problem solved
[root@bogon ~]# ps
PID TTY TIME CMD
3930 pts/1 00:00:00 bash
11734 pts/1 00:00:00 ps
[1]+ Killed ./a.sh
[root@bogon ~]# ./a.sh &
[1] 11735
[root@bogon ~]# ps
PID TTY TIME CMD
3930 pts/1 00:00:00 bash
11735 pts/1 00:00:01 a.sh
11736 pts/1 00:00:00 ps
[root@bogon ~]# kill 11735 # Normal background programs can be run directly kill
[root@bogon ~]# ps
PID TTY TIME CMD
3930 pts/1 00:00:00 bash
11737 pts/1 00:00:00 ps
[1]+ Terminated ./a.sh
[root@bogon ~]#