[LINUX] Commands that operate processes ~ Process status check, priority, background execution, etc.

I will briefly introduce the commands that manipulate the process.

Please use it for learning, LPIC, LinuC measures, etc.

Also, if you think it's processed cheese, I highly recommend browser back.

Command list

A program running on Linux is called a process.

The following commands that handle such a process are introduced with usage examples. (The one in parentheses is a bonus)

command Description
pgrep Search for the running process ID based on username, UID, GID, etc.
top View running processes in real time
ps Get process information
pstree View currently running processes in a tree format
nice Change process priority
renice Change the priority of running processes
jobs View running processes
bg Make it a background job
fg Make it a foreground job
kill Send a signal to the process
killall Send a signal to the process
(free) Show the amount of free and used memory in the system

pgrep

A command to search for a running process ID based on user name, UID, GID, etc.

$ pgrep -u root
1
2
3
4
6
...(abridgement)
$ pgrep -U 0
1
2
3
4
6
...(abridgement)

You can specify the user name with -u and the UID with -U.

Root UID is 0 in LINUX

top

A command to display the running process in real time

top - 22:06:29 up 1 day, 16:36,  1 user,  load average: 0.00, 0.02, 0.00
Tasks:  96 total,   1 running,  55 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1002108 total,   150664 free,   517700 used,   333744 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   330584 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                           
17870 mysql     20   0 1309152 319372      0 S  0.7 31.9   6:43.18 mysqld                                            
32132 ubuntu    20   0   44524   3928   3320 R  0.3  0.4   0:00.01 top                                               
    1 root      20   0  159948   6360   3880 S  0.0  0.6   0:04.36 systemd                                           
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                                          
    3 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 rcu_gp

Running ps with no options shows only processes launched from the current shell You can also see the mysqld that I used to play in my environment and the process of the top command that I'm running now. The screen is automatically updated and displayed in descending order of CPU usage Monitoring ends when you press "q"

"PID" means the process ID, "USER" means the user who executed the process, "% CPU" means the percentage of CPU consumed, and "% MEM" means the percentage of memory consumed.

ps

PS (ProceSs) command is a command to get process information

$ ps
  PID TTY          TIME CMD
31249 pts/0    00:00:00 bash
31348 pts/0    00:00:00 ps

If you execute without options, a list of processes started on that terminal will be displayed. "PID" is the process ID, "TTY" is the name of the terminal that executed the process, "TIME" is the execution time of the process, and "CMD" is the execution command. From the results, we can see that the shell uses bash.

option Description
a View other users' processes
u Also display the user name
x Also show processes that are not executed from the terminal
e Display information about all processes except kernel processes
f Generate a complete list
o Customize the columns to display

Show active processes

ps aux

$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.6 159948  6268 ?        Ss   Aug27   0:04 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Aug27   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   Aug27   0:00 [rcu_gp]
...abridgement...
ubuntu   31248  0.0  0.3 107980  3628 ?        S    14:17   0:00 sshd: ubuntu@pts/0
ubuntu   31249  0.0  0.5  23112  5064 pts/0    Ss   14:17   0:00 -bash
ubuntu   31351  0.0  0.3  40088  3504 pts/0    R+   14:22   0:00 ps aux

ps -efThere is also a method

$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Aug27 ?        00:00:04 /sbin/init
root         2     0  0 Aug27 ?        00:00:00 [kthreadd]
root         3     2  0 Aug27 ?        00:00:00 [rcu_gp]
...abridgement...
ubuntu   31248 31155  0 14:17 ?        00:00:00 sshd: ubuntu@pts/0
ubuntu   31249 31248  0 14:17 pts/0    00:00:00 -bash
ubuntu   31355 31249  0 14:27 pts/0    00:00:00 ps -ef

It's nice to customize the display as you like

$ ps -o pid,nice,user,cmd
  PID  NI USER     CMD
 3614   0 ubuntu   -bash
 4341   0 ubuntu   ps -o pid,nice,user,cmd

pstree

A command to display the currently running process in a tree format

Below is an example of running on AWS EC2

$ pstree
systemd─┬─accounts-daemon───2*[{accounts-daemon}]
        ├─acpid
        ├─2*[agetty]
        ├─amazon-ssm-agen───8*[{amazon-ssm-agen}]
        ├─atd
        ├─containerd───8*[{containerd}]
        ├─cron
        ├─dbus-daemon
        ├─dockerd───9*[{dockerd}]
        ├─lvmetad
        ├─lxcfs───10*[{lxcfs}]
        ├─mysqld───38*[{mysqld}]
        ├─networkd-dispat───{networkd-dispat}
        ├─polkitd───2*[{polkitd}]
        ├─rsyslogd───3*[{rsyslogd}]
        ├─snapd───8*[{snapd}]
        ├─sshd───sshd───sshd───bash───pstree
        ├─systemd───(sd-pam)
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-network
        ├─systemd-resolve
        ├─systemd-timesyn───{systemd-timesyn}
        ├─systemd-udevd
        └─unattended-upgr───{unattended-upgr}

You can see that bash is started from sshd required to access EC2 and pstree is running from that bash

nice

Change process priority

Process priorities are defined as -20 from -20 to 19, with lower priorities being higher.

When executed with no arguments, the priority is incremented by 10.

$ sleep 15 &
[2] 2644
[1]   Done                    nice sleep 15
$ ps -o pid,nice,user,cmd
  PID  NI USER     CMD
 2521   0 ubuntu   -bash
 2644   0 ubuntu   sleep 15
 2645   0 ubuntu   ps -o pid,nice,user,cmd
$ nice sleep 15 &
[1] 2634
$ ps -o pid,nice,user,cmd
  PID  NI USER     CMD
 2521   0 ubuntu   -bash
 2634  10 ubuntu   sleep 15
 2635   0 ubuntu   ps -o pid,nice,user,cmd

renice

Change the priority of running processes

$ sleep 15 &
[1] 2651
$ ps -o pid,nice,user,cmd
  PID  NI USER     CMD
 2521   0 ubuntu   -bash
 2651   0 ubuntu   sleep 15
 2652   0 ubuntu   ps -o pid,nice,user,cmd
$ renice 10 2651
2651 (process ID) old priority 0, new priority 10
$ ps -o pid,nice,user,cmd
  PID  NI USER     CMD
 2521   0 ubuntu   -bash
 2651  10 ubuntu   sleep 15
 2655   0 ubuntu   ps -o pid,nice,user,cmd

jobs

(Background) View running jobs. A job is a unit of processing seen by the user, and there are foreground jobs that are visible to the user and background jobs that are invisible to the user. You can make it a background job by prepending the bg command or prepending the "&" to the execution command.

$ sleep 60 &
[1] 3810
$ jobs
[1]+  Running                 sleep 60 &
$ kill -s SIGKILL %1
$ jobs
[1]+  Killed                  sleep 60

[1]Or%1Is called the job id and is assigned to each user.

bg

Make it a background job

Type Ctrl + z in the running program, or &Even if it is executed with, it becomes a background job.

fg

Make it a foreground job

In bash, only one program can run in the foreground job

kill

You can use the kill command to signal a process

Signal ID Signal name motion
1 SIGUP Termination by disconnecting the terminal
2 SIGINT End by interrupt
9 SIGKILL forced termination
15 SIGTERM Exit (default)
18 SIGCONT Resume

If it is SIGTERM, the program termination process is executed, but SIGKILL is forcibly terminated as it is.

By the way, SIGINT is sent when ending with "Control + c"

End the process

If you want to terminate the process with process ID 32147, do as follows

$ kill -15 32147

You can also end by specifying the signal ID

$ kill -s SIGTERM 32147

Kill the process

Specify the signal ID

$ kill -9 32147

Specify the signal name

$ kill -s SIGKILL 32147

killall

Send a signal to the process by specifying the process name

Even if the same program is executed multiple times, different PIDs are assigned to each process, but if killall is used, processes with the same name can be terminated at once.

$ vi a.txt &
[1] 3728
$ ps
  PID TTY          TIME CMD
 3614 pts/0    00:00:00 bash
 3728 pts/0    00:00:00 vi
 3732 pts/0    00:00:00 ps
$ killall -15 vi
$ ps
  PID TTY          TIME CMD
 3614 pts/0    00:00:00 bash
 3728 pts/0    00:00:00 vi
 3734 pts/0    00:00:00 ps
$ killall -SIGKILL vi
[1]+  Killed                  vi a.txt
$ ps
  PID TTY          TIME CMD
 3614 pts/0    00:00:00 bash
 3736 pts/0    00:00:00 ps

free

Show the amount of free and used memory in the system

When combined with the -t option, it also displays a line showing the total of physical memory and swap memory.

$ free -t
              total        used        free      shared  buff/cache   available
Mem:        1002108      516296      104900         812      380912      331188
Swap:             0           0           0
Total:      1002108      516296      104900

Summary

You can get to know the kernel program (at least in terms) just by elaborating on the process. Let's live a fun Linux life while being aware of what programs are running on your computer.

It was a topic about the process for the first time in a year. Nice to meet you too.

How to end the process ~ top & kill

Recommended Posts

Commands that operate processes ~ Process status check, priority, background execution, etc.