1
头图

foreword

A process is a running program, and Linux systems typically have hundreds of processes running at the same time. This article will introduce how Linux processes process management.

We can see that:

  • Viewing processes
  • Finding processes
  • Managing processes
  • Prioritizing processes
  • Killing processes
  • Running processes in the background
  • Scheduling processes

View progress

ps

When the Linux kernel creates a process, it sequentially assigns a unique process ID (PID) to each process. Generally, to perform any operation on a process, we have to specify the PID, sometimes we can use the name.

ps command is the main tool for viewing processes. Running this command without any options lists processes started (invoked) by the currently logged-in user, as well as processes running on the terminal.

If we add the aux option:

  • a = show processes for all users
  • u = show the user/owner of the process
  • x = show processes not connected to a terminal

image.png

Processes are displayed in the order they were started, so you'll see the list sorted by PID.

Let's briefly look at the information in the following columns:

  • USER : The user of the calling process.
  • PID : The ID of the process.
  • %CPU : The percentage of CPU occupied by the process.
  • %MEM : The percentage of memory occupied by the process.
  • COMMAND : The command to start the process.

find command

grep

We can use the grep command to find a specified process, let's say we want to find all processes whose name contains mfsconsole .

 ps aux | grep msfconsole

image.png

top

We can use the top command to dynamically list the processes sorted by resource usage, starting with the largest. By default, the list will be refreshed every 3 seconds.

 top

image.png

management process

priority

The kernel has the final say on the priority of a process, but we can use the nice command to suggest that the priority of a process should be raised. nice The value of the command ranges from -20 (most likely to get priority) to +19 (least likely to get priority).

Higher nice value is converted to low priority, lower nice value is converted to high priority.

When a process is started, all standard processes are started with a value of nice value 0.

nice命令来设置一个进程启动时的nice值, renice来调整一个运行中的进程的nice .

When we start a process with the nice command without providing any value, the default nice value is 10.

nice command requires you to increment the nice value, renice command wants an absolute nice value.

set priority

We can use the nice command to adjust a program's nice value. This allows us to raise or lower the priority given to this process by the kernel, relative to other processes.

On the left, I execute watch -n1 free to show the details of system memory usage. top ,我让---b6a9f9c6779d658e3d4b4d2a864d8e46---命令运行,你watch命令的PID是9717, nice 0。

image.png

Let's continue with the watch command, only this time with the nice command.

 nice -15 watch -n1 free

image.png

Now we can see that watch has a value of 15 for nice . A few things to keep in mind here:

  • This watch command PID is different from the previous watch command. This is because nice will start a new process, not change an existing one.
  • nice after the command -15 means 15 . If we want to specify a negative number (high priority), we can use double dashes - .
  • Here is what it looks like after executing sudo nice --10 watch -n1 free . Yes, if you want to increase the priority you have to use sudo . Anyone can lower the priority, but only sudo can raise the priority.

image.png

change priority

renice command receives the absolute value of -20 to 19 and receives the process's PID .

Let's run watch command again.

 watch -n1 free

Let's check the nice value, since we didn't specify nice value, it should be 0. Instead of using top , here I will use ps and grep to simplify the output.

image.png

We can see that the value of the eighth column is 0, which is the value of nice , and the value of PID is in the third column. Let's try it with renice :

 sudo renice -15 14318

image.png

We can see that the value of nice is now -15. We can also use the top tool to change the nice value.

In top , press the R key and provide a PID :

image.png

Press enter and provide a new nice value:

image.png

top successfully changed nice value:

image.png

kill process

kill

You can stop a problematic process with the kill command. kill command has 64 different kill signals, and the syntax is kill -signal PID . If no signal bit is provided, it will default to SIGTERM . Here I will focus on the following:

Signal name Numerical value describe
SIGHUP 1 Hang up (HUP) signal. It stops the specified process and restarts it with the same PID.
SIGINIT 2 Interrupt (INT) signal. This is a weak kill signal, not guaranteed to work, but it does happen.
SIGQUIT 3 core dump. Terminates the process and saves the process information in memory, then it saves this information in a file called core in the current working directory.
SIGTERM 15 Terminate (TERM) signal. It is the default kill signal for the kill command.
SIGKILL 9 This is an absolute kill signal. It forces the process to stop by sending its resources to a special device /dev/null.

The following command will restart our watch command via the HUP signal.

 kill -1 14318

The following command will ensure that the process is terminated.

 kill -9 16318

If we don't know PID , we can use the killall command, which receives the name of the process.

 killall -9 watch

image.png

Run a process in the background

&

When you execute a command, shell waits until the command completes before providing another command prompt. We can run a process in the background and it will continue to run without the need for a terminal, freeing up the terminal for other work. We do this by adding & to the end of the command.

 geany sample.txt &

image.png

geany no longer takes up the entire terminal.

fg

How do we bring it back to the foreground? You can use the fg command with the PID.

 fg 18345

bg

You can also use the bg command to move a process to the background.

 bg 18345

scheduler process

In Linux, we can use at and crond to schedule processes. crond is a bit complicated, and here we focus on at .

at

at command is useful for scheduling a job to run once at some point in the future. It sets up the atd daemon. A daemon is a program that sits in the background and does its own thing without any user interface.

Below is the syntax of the at command execution time of the process, the time can be provided in a variety of formats.

Time format meaning
at 7:20pm Run at 7:20 PM of current day.
at 7:20pm June 25 Run at 7:20 PM on June 25
at now + 20 minutes Run in 20 minutes
at 7:25pm 06/10/2021 Run at 7:25 pm on June 10, 2021
 at noon

image.png

We can see that at brings us into interactive mode, where we enter the command we want to execute at the specified time. Press CTRL+D when done.

atq

Use atq to list all scheduled at jobs.

image.png


chuck
303 声望41 粉丝