linux command - process (service) management

Keywords: Linux shell

1. ps process to view the instantaneous process status (use the top command to view the real-time process)

ps supports three formats of commands
1) Unix style, options start with -.
2) BSD style, options cannot start with -.
3) GNU long option, which starts with -.
Commands of the three formats can be mixed, but there may be conflicts. Use each option flexibly to display the output you want.

By default, ps selects all processes that have the same valid user ID (euid = EUID) as the current user and are associated with the caller on the same terminal. It displays the process ID (pid = PID), the terminal associated with the process (tname = TTY), the accumulated CPU time in [DD-] hh: mm: ss format (time = TIME) and the executable name (ucmd = CMD). By default, the output is not sorted.

hollowman@hollowman-F117:~$ ps
  PID TTY          TIME CMD
40964 pts/3    00:00:00 bash
40970 pts/3    00:00:00 ps

Common options

#Unix style options
 -A, -e                Show all processes
 -a                    Show all TTY Process with existing value, remove bash and TTY Terminal and other processes
 -u                    Displays a valid user when followed by<UID>Same meaning when used-U. 
 -d                    Show all processes, remove bash and TTY Terminal and other processes
 -F                    Full format
 -f                    Full format, including command line

#BSD style options
  a                    Show all TTY Processes for which values exist, including other users
  u                    A user oriented format whose output contains a user name field

#Options with the same meaning
 -x, x                 Displays processes without control terminals. Generally, processes without control terminals are not displayed by default
 -t, t/T, --tty <tty>  Displays the processes associated with the specified terminal
 -p, p, --pid <PID>    Displays the process with the specified process number
 -r, r                 Show running processes
 -w, w                 Unlimited output width,By default, output beyond the width of the device is not displayed.
 -U, U, --user <UID>   Displays the processes for the specified user

#Similar options
 -l, l                 Display long format/BSD Long format
 -j, j                 Display working format/BSD Working format

Process status:

  • R (run): the process is running or waiting in the run queue.
  • S (interrupt): the process is in sleep. When a condition is formed or a signal is received, it will leave the state.
  • D (non interruptible): the process does not respond to the system asynchronous signal, and it cannot be interrupted even with the kill command.
  • Z (dead): the process has been terminated, but the process descriptor still exists until the parent process releases the process after calling the wait4() system function.
  • T (stop): the process stops running after receiving the stop signal
  • N: Low priority
  • <: high priority
  • 50: L ocked into memory
  • s: Include child processes
  • l: Multithreading

Common output formats
1. Print all process information
ps -ef
ps aux #ps auxg and PS – e u output have the same effect

2. Print process tree
ps -ejH
ps axjf

3. Print thread information
ps -eLf
ps axms dynamic display process

The first five lines of the top command dynamically display the overall system operation parameters:

  • Line 1: system time, running time, number of login terminals and system load (the three values are the average values within 1 minute, 5 minutes and 15 minutes respectively. The smaller the value, the lower the load).
  • Line 2: total number of processes, running processes, sleeping processes, stopped processes and dead processes.
  • Line 3: percentage of resources occupied by users, percentage of resources occupied by system kernel, percentage of process resources whose priority has been changed, percentage of idle resources.
  • Line 4: total physical memory, free memory, memory usage, memory used as kernel cache.
  • Line 5: total virtual memory, virtual memory free, virtual memory usage, and memory that has been loaded in advance.
hollowman@hollowman-F117:~$ top

top - 08:22:14 up 29 min,  1 user,  load average: 0.30, 0.38, 0.29
 task: 369 total,   1 running, 364 sleeping,   2 stopped,   2 zombie
%Cpu(s):  2.5 us,  1.1 sy,  0.0 ni, 96.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  15927.6 total,   9434.5 free,   2515.6 used,   3977.5 buff/cache
MiB Swap:  16384.0 total,  16384.0 free,      0.0 used.  12548.7 avail Mem 

 process USER      PR  NI    VIRT    RES    SHR    %CPU  %MEM     TIME+ COMMAND 
 8116 hollowm+  20   0 2625220 170932 112556 S  11.6   1.0   1:32.42 Web Content
 1217 root      20   0 1305940 124032  79068 S   9.3   0.8   0:53.46 Xorg
 2115 hollowm+  20   0 3432152 128492  95312 S   6.6   0.8   0:27.11 ukui-kwin_x11
 2813 hollowm+  20   0  610712  79224  59364 S   4.0   0.5   0:07.34 mate-terminal 
 7654 hollowm+  20   0 5857576 807552 509932 S   4.0   5.0   1:11.70 firefox
 1916 hollowm+   9 -11 1412544  19728  14980 S   2.0   0.1   0:29.13 pulseaudio 
 2826 hollowm+  20   0 2224664  70504  30216 S   1.7   0.4   0:28.18 kylin-kmre-apps  
  410 root      19  -1  119080  47784  46228 S   1.3   0.3   0:23.46 systemd-journal 

3. pstree process tree to display the process tree.

Several common options

  -s, --show-parents  Displays the parent process of the selected process
  -t, --thread-names  Displays the complete thread name. By default, the thread name is not displayed(Show only the number of threads)
  -c, --compact-not   Do not compress identical subtrees
  -p, --show-pids     display PID;Implicit enable -c option
  -a, --arguments     Display command line parameters
  -V, --version       display version information


#The process tree is displayed. By default, it starts from the process with pid 1, that is, the systemd process.
hollowman@hollowman-F117:~$ pstree
        ... ...

#Display the process tree with pid 889 (compress the same subtree)
hollowman@hollowman-F117:~$ pstree 889

#The process tree with pid 889 is displayed, and the identical subtree is not compressed
hollowman@hollowman-F117:~$ pstree 889 -c

#Displays the process tree with pid 889 and contains the thread name
hollowman@hollowman-F117:~$ pstree 889 -t

#Display the process tree and process number with pid 889
hollowman@hollowman-F117:~$ pstree 889 -p

#Display the process tree and process number with pid 889, and display the parent process name and process number
hollowman@hollowman-F117:~$ pstree -s 889 -p

#The process tree with pid 1543 is displayed, and the command line parameters are displayed
hollowman@hollowman-F117:~$ pstree 1543 -a
smbd --foreground --no-process-group
  ├─cleanupd --foreground --no-process-group
  ├─lpqd --foreground --no-process-group
  └─smbd-notifyd --foreground --no-process-group

#Displays the hollowman user's process and its process tree
hollowman@hollowman-F117:~$ pstree hollowman


Query the pid values of all processes of a specified service. A service corresponds to 1 or more processes, but each process has only a unique pid value.
Command syntax: pidof [options] [service name]

hollowman@hollowman-F117:~$ pidof code  #Code is the name of the running program of a code editor
32690 32674 32659 31492 31481 31479 31451 31450 31447

5. kill terminates the service process corresponding to the pid value

Command syntax: kill [options] [pid]

hollowman@hollowman-F117:~$ kill 31450

6. Kill terminates all processes corresponding to the specified service / program

Kill can only kill one process. Services containing multiple processes need to use kill multiple times, while kill can kill all processes corresponding to the service at one time
Command syntax: kill [options] [service / program name]

hollowman@hollowman-F117:~$ killall code   #Close the code program

Posted by wxflint on Sun, 26 Sep 2021 18:21:56 -0700