[Linux] System management processes and scheduled tasks

1. Process management

The relationship between processes, threads and coroutines

Program: A sequence of instructions or code that a computer can recognize

Process: Run the code

Thread: A thread is the smallest unit that the operating system can perform operation scheduling. It is included in the process and is the actual operating unit in the process.

Coroutine: a lightweight thread in a thread written by programmers

The difference between processes and threads

Thread is the smallest unit of program execution, and process is the smallest unit of operating system resource allocation; a process consists of one or more threads, which are different paths for code execution in a single process; although processes are independent of each other, under the same process Each thread of the program shares the memory space of the program (including code segments, data sets, heaps, etc.) and certain process-level resources (such as open files). When sharing process-level resources, threads within a process are invisible to other processes; Scheduling and switching: Thread context switching is much faster than process context switching.

2. Check the progress

2.1 Static viewing process–ps aux/ps -elt

Options:

Options Function
a Displays all process information under the current terminal, including processes of other users. When combined with the “x” option, all process information in the system will be displayed.
u Output process information in a user-oriented format.
x Display process information of the current user in all terminals.
-e Displays all process information in the system.
-l Use long format to display process information.
-f Display process information in full format
ps aux process information explanation
Name Explanation
USER The user name that created the process
PID The id of the process, assigned by the system
%CPU The percentage of CPU resources occupied by the process. The higher the percentage, the more resources the process consumes
%MEM The percentage of physical memory occupied by the process. The higher the percentage occupied, the more resources the process consumes
VSZ The size of the virtual memory occupied by this process, in KB
RSS The actual physical memory occupied by the process, in KB
TTY The process is running in the terminal
STAT < strong>The status of the process
START The startup time of the process strong>
TIME This process takes up CPU computing time, please note that it is not system time
COMMAND The command name that generated this process

Supplement: Type of STAT status

-D: Sleep state that cannot be woken up, usually used for I/O situations.

-R: The process is running.

-S: The process is in sleep state and can be awakened.

-T: Stop state, which may be paused in the background or the process is in a debug state.

-Z: Zombie process. The process has been terminated, but hardware resources are still occupied.

Supplement 2: Terminal for running TTY

tty1~tty6 are local character interface terminals

tty7 is a graphical terminal

ps -elf

Column name Explanation
F Process flag, indicating the permissions of the process. There are two common flags: 1: The process can be copied, but cannot be executed; 4: Process Use superuser privileges;
S Process status. The specific status is consistent with the STAT status in the “psaux” command;
UID The ID of the user running this process;
PID The ID of the process;
PPID The ID of the parent process;
C The CPU usage of the process, the unit is percentage;
PRI The priority of the process. The smaller the value, the higher the priority of the process and the earlier it is executed by the CPU; the system definition cannot be modified manually
NI The priority of the process, the smaller the value, the earlier the process is executed; it can be modified manually
ADDR Which part of the memory is the process in? Location;
SZ How much memory does this process occupy;
WCHAN Whether the process is running. “-” means running;
TTY Which terminal is the process generated from?
TIME This process occupies the computing time of the CPU, please note that it is not the system time;
CMD The command name that generates this process;

The reason for the zombie state: The parent process exits before the child process, resulting in the child process being unable to be recycled and unable to run.

2. 2 View thread– ps -aT

2.3 Dynamically view the process – top

Options:

Options Function
-d Specifies that the top command is updated every few seconds. Default is 3 seconds
-b Use batch mode output. Generally used with the “-n” option to redirect the top command to a file
-n < strong>Specify the number of times the top command is executed. Generally used with the “-” option
-p Only view processes with the specified ID
-s Enable the top command to run in safe mode to avoid errors in interactive mode
-u Only monitor the process of a certain user

In the display window of the top command, you can also use the following keys to perform interactive operations:

? or h: Display help in interactive mode;

c: Sort by CPU usage, this option is the default;

M: Sort by memory usage;

N: Sort by PID;

T: Sort by the cumulative operation time of the CPU, that is, sort by TIME + items;

k: Give a signal to a process according to PID. Generally used to terminate a process, signal 9 is a forced termination signal;

r: Reset the priority (Nice) value of a process according to its PID;

q: Exit the top command;

2.4 View processes based on conditions

-U: Specify user

-l: Display process name

-a: Display the full format of the process name

-P : Display the child processes of the specified process

-t View terminal

2.5 Tree View Process–pstree

Options:

-p: Display PID

2.6 View the specified process – prtstat

3. Manual control process

The startup of the process is divided into foreground startup and background startup

Foreground startup: After the user enters a command, the command is launched directly on the page

Background startup: When the operation required by the command is too large and occupies the terminal, causing the user to be unable to enter other commands again, the processing process is placed in the background and other operations can be performed at the same time

3.1 Transfer the foreground command process to the background– & amp;

3.2 Front and backend scheduling of processes

jobs command: jobs [-l] View the list of tasks in the background;

Do not display process ID

jobs -l can display the process number

fg command: restore the background process to the foreground and specify the task number;

3.3 Terminate the running of the process kill

Ctrl + C key combination: interrupt the command being executed;

kill, killall command

kill is used to terminate the process with the specified PID number

killall is used to terminate all processes related to the specified name

The -9 option is used to force termination

kill <process name> //Kill a certain process
kill -9 <PID> //Force kill
killall <process name> //Kill all processes

Commands to view terminal information (who, w, ps aux);

4. Planned task management

4.1 at command

One-time scheduled tasks

The task set using the at command will only be executed once at the specified time point. If only the time is specified, it means that time of the day. If only the date is specified, it means the current time of the date;

You can enter multiple commands in the at interactive environment, and finally press Ctrl + D to submit;

at [HH:MM] [yyyy-mm–dd]: Set a one-time scheduled task at a specific time ##year##month##日##hour## minute;

[root@localhost ~]# atq //View unexecuted plans
1 Mon Nov 6 18:42:00 2023 a root

[root@localhost ~]# at 1:00 2024-1-1 #Set a one-time plan at 1:00 on 2024-01-1
at> echo 'happy' > /opt/happy.txt
at> <EOT>
job 3 at Mon Jan 1 01:00:00 2024
[root@localhost ~]# atq #View plans that have not yet been run in the system
2 Mon Jan 1 00:00:00 2024 a root
3 Mon Jan 1 01:00:00 2024 a root
[root@localhost ~]#
[root@localhost ~]# atrm 2 #Delete the plan with serial number 2 in the system
[root@localhost ~]# atq #View the plans that have not yet been executed in the system at this time
3 Mon Jan 1 01:00:00 2024 a root
[root@localhost ~]#

4.2 crontab command

crontab opens a configuration file like a vim editor and writes periodic tasks to the configuration file. However, writing periodic tasks requires understanding and mastering the use of periodic time

Options:

Options Function
-u Used to set up a user’s crontab service
-e Edit the contents of a user’s crontab file. If no user is specified, it means editing the current user’s crontab file.
-l Display the contents of a user’s crontab file. If the user is not specified, It means displaying the contents of the current user’s crontab file.
-r Delete a user’s crontab file from /var/spool/cron , if no user is specified, the current user’s crontab file will be deleted by default.
-i Give a confirmation prompt when deleting the user’s crontab file.

Format of editing tasks:

* Points 0~59
* hours 0~23
* days 1~31
* Month 1~12
* Week 0~7 (0 and 7 both represent Sunday)
Example:

  0 8-18/2 * * * //Every 2 hours between 8 o'clock and 18 o'clock

  0 * */3 * * //Every hour of every 3 days

  0 14-18 1,5,15,30 * * // Executed on the 1st, 5th, 15th, and 30th of each month from 2 to 6 p.m.

  15-45/5 10,22 */10 10 * // Execute every 5 minutes every 10 days in the morning and 10pm every 15-45 minutes in October
Symbol Function
* (asterisk) represents any time. For example, the first “*” means that it will be executed every minute for an hour.
, (comma) represents discontinuous time. For example, “0 8, 12, 16*** command” means that the command is executed at 8:00, 12:00, and 16:00 every day.
– (middle bar) represents a continuous time range. For example, “0 5 ** 1-6 command” means executing the command at 5:00 am from Monday to Saturday.
/ (forward slash) represents how often it is executed. For example, “*/10 command” means executing the command every 10 minutes.

which command //View the absolute path of the command

crontab -e //Create a periodic task
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
*/1 * * * * /usr/bin/cp /etc/passwd /opt/passwd1 //Copy every minute
wq //Exit save

[root@localhost opt]# crontab -l //View periodic tasks
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin //Define the location of the software
*/1 * * * * /usr/bin/cp /etc/passwd /opt/passwd1 //Copy the file under etc to opt and rename it
[root@localhost opt]#
[root@localhost opt]# ls
happy.txt passwd passwd1 rh 

4.3 Deletion of periodic tasks

Summary

Static viewing: ps aux, ps -elf

Dynamic view: top

View the specified process: prtstat

List in tree structure: pstree

at one-time task settings

Be careful with -9 when killing processes. Use it with caution. Improper use may lead to data loss

When planning periodic tasks, pay attention to the use of periodic methods, make good use of periodic symbols, and set the required execution period

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skill treek8s package management (helm)Installation helm16941 people are learning the system