Skip to content

Commit d595b51

Browse files
authored
Format fixes and some additional knowledge points (#1857)
* Format fixes and some additional knowledge points * add pstree command
1 parent 4d0ddf0 commit d595b51

File tree

1 file changed

+95
-4
lines changed

1 file changed

+95
-4
lines changed

docs/books/admin_guide/08-process.md

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,110 @@ The `pkill` command will send the specified signal (by default _SIGTERM_) to eac
378378

379379
```
380380
pgrep process
381-
pkill [-signal] process
381+
pkill [option] [-signal] process
382382
```
383383

384384
Examples:
385385

386386
* Get the process number from `sshd`:
387387

388+
```
389+
$ pgrep -u root sshd
390+
```
391+
392+
* Kill all `tomcat` processes:
393+
394+
```
395+
$ pkill tomcat
396+
```
397+
398+
!!! note
399+
400+
Before you decide to kill a process, it's best to know exactly what the process is for, otherwise it can lead to system crashes or other unpredictable problems.
401+
402+
In addition to sending signals to the relevant processes, the `pkill` command can also end the user's connection session according to the terminal number, such as:
403+
388404
```
389-
$ pgrep -u root sshd
405+
$ pkill -t pts/1
390406
```
391407

392-
* Kill all `tomcat` processes:
408+
### `killall` command
409+
410+
The function of this command is roughly the same as that of the `pkill` command. The usage is - `killall [option] [ -s SIGNAL | -SIGNAL ] NAME`. The default signal is _SIGTERM_.
411+
412+
| Options | Description |
413+
| :--- | :--- |
414+
| `-l` | list all known signal names |
415+
| `-i` | ask for confirmation before killing |
416+
| `-I` | case insensitive process name match |
417+
418+
Example:
419+
420+
```
421+
$ killall tomcat
422+
```
423+
424+
### `pstree` command
425+
426+
This command displays the progress in a tree style and its usage is - `pstree [option]`.
427+
428+
| Option | Description |
429+
| :--- | :--- |
430+
| `-p` | Displays the PID of the process |
431+
| `-n` | sort output by PID |
432+
| `-h` | highlight current process and its ancestors |
433+
| `-u` | show uid transitions |
434+
435+
```bash
436+
$ pstree -pnhu
437+
systemd(1)─┬─systemd-journal(595)
438+
├─systemd-udevd(625)
439+
├─auditd(671)───{auditd}(672)
440+
├─dbus-daemon(714,dbus)
441+
├─NetworkManager(715)─┬─{NetworkManager}(756)
442+
│ └─{NetworkManager}(757)
443+
├─systemd-logind(721)
444+
├─chronyd(737,chrony)
445+
├─sshd(758)───sshd(1398)───sshd(1410)───bash(1411)───pstree(1500)
446+
├─tuned(759)─┬─{tuned}(1376)
447+
│ ├─{tuned}(1381)
448+
│ ├─{tuned}(1382)
449+
│ └─{tuned}(1384)
450+
├─agetty(763)
451+
├─crond(768)
452+
├─polkitd(1375,polkitd)─┬─{polkitd}(1387)
453+
│ ├─{polkitd}(1388)
454+
│ ├─{polkitd}(1389)
455+
│ ├─{polkitd}(1390)
456+
│ └─{polkitd}(1392)
457+
└─systemd(1401)───(sd-pam)(1404)
458+
```
459+
460+
### Orphan process and zombie process
461+
462+
**orphan process**: When a parent process dies, its children are said to be orphans. These special state processes are adopted by the init process and status collection is completed until they are destroyed. Conceptually speaking, the orphanage process does not pose any harm.
463+
464+
**zombie process**: After a child process completes its work and is terminated, its parent process needs to call the signal processing function wait() or waitpid() to obtain the termination status of the child process. If the parent process does not do so, although the child process has already exited, it still retains some exit status information in the system process table, because the parent process cannot obtain the status information of the child process, these processes will continue to occupy resources in the process table. We refer to processes in this state as zombies.
465+
466+
Hazard:
467+
468+
* Occupying system resources and causing a decrease in machine performance.
469+
* Unable to generate new child processes.
470+
471+
How to check if there are any zombie processes in the current system?
393472

394473
```
395-
$ pkill tomcat
474+
$ ps -lef | awk '{print $2}' | grep Z
396475
```
476+
477+
These characters may appear in this column:
478+
479+
* **D** - uninterruptible sleep (usually IO)
480+
* **I** - Idle kernel thread
481+
* **R** - running or runnable (on run queue)
482+
* **S** - interruptible sleep (waiting for an event to complete)
483+
* **T** - stopped by job control signal
484+
* **t** - stopped by debugger during the tracing
485+
* **W** - paging (not valid since the 2.6.xx kernel)
486+
* **X** - dead (should never be seen)
487+
* **Z** - defunct ("zombie") process, terminated but not reaped by its parent

0 commit comments

Comments
 (0)