Process and scheduled task management related commands (how to view process information, process start and termination, scheduled task management)

1. The relationship between procedures and processes

1.1 Procedure

  • Executable code and data stored in the hard disk, CD, etc.
  • Statically saved code in the file

1.2 Process

  • Program code running in CPU and memory
  • Dynamically executed code
  • Parent and child processes
    -each program can create one or more processes

1.3 ps command-view process information

1.3.1 View static process statistics

ps to

a:
Display all processes on the terminal_, including processes of other users
u:
Display the processes of all terminals
x:
Display the processes
Insert picture description here
of all terminals Explanation of each column in the above figure:

  • USER:
    the user of the process
  • PID:
    ID of the process
  • %CPU:
    The percentage of CPU occupied by the process
  • %MEM:
    percentage of memory occupied
  • VSZ:
    The amount of virtual memory used by the process (KB)
  • RSS:
    The amount of physical memory occupied by the process (KB)
  • TTY:
    The name of the terminal that started the process. Processes that are not started from the terminal are displayed as?
  • STAT:
    the status of the trip (D: non-interruptible sleep status; R: running status S: in sleep status and can be awakened; T: stop status, which may be suspended in the background or the process is in a tracking and debugging status; z: zombie Factory process, the process has been terminated, but some programs are still in memory)
  • START:
    The process was triggered to start time
  • TIME:
    The time the process actually uses the CPU to run
  • COMMAND:
    the start command of the
    process (zombie process: a process ends, but if the parent process of the process has ended first, then the process will not become a zombie process, because when each process ends, the system will scan the current For all processes running in the system, see if any process is a child process of the process that just ended. If so, Init will take over and become its parent process. After the child process exits, init will reclaim its occupied process. Related resources.
    But if the child process ends before the parent process, and the parent process does not reclaim the child process and release the resources occupied by the child process, the child process will become a zombie process.)

1.3.2 View static process statistics

ps -elf

-e:
display all process information in the system
-l:
use long format to display process information
-f:
use complete format to display process information

Insert picture description here
Explanation of each column in the above figure:

  • F:
    System tag assigned by the kernel to the process
  • S:
    The status of the process
  • UID:
    the user who started these processes
  • PID:
    Process ID of the process
  • PPID:
    The process number of the parent process (if the process was started by another process)
  • C:
    CPU utilization in the life cycle of the process
  • PRI:
    The priority of the process (the larger the number, the lower the priority)
  • NI:
    Moderate value is used to participate in determining priority
  • ADDR:
    the memory address of the process
  • SZ:
    If the process is swapped out, the approximate size of the swap space required
  • WCHAN:
    If the process is sleeping, display the name of the system function in sleep
  • STIME:
    System time when the process started
  • TTY:
    The terminal device when the process starts
  • TIME:
    Cumulative CPU time required to run the process
  • CMD:
    The start command of the process

1.4 top command-view process information

The top command is opposite to the ps command. The top command can view dynamic process information. The
Insert picture description here
first line is task queue information:
Insert picture description here

  • 14:57:11-system time
  • up 4:23-the length of time the system has been running
  • 4 users-the number of currently logged in users
  • Load average: 0.00 0.01 0.05 ——System load, that is, the number of tasks processed by the system in a unit time, the last three numbers are the average value from 1 minute, 5 minutes, and 15 minutes to the present

The second line of process information:
Insert picture description here

  • tasks-total number of processes
  • running-the number of running processes
  • sleeping-the number of sleeping processes
  • stopped-the number of processes stopped
  • zombie-the number of zombie processes

The third line of CPU information:

Insert picture description here

  • us ——User occupied
  • sy-kernel occupation
  • ni ——Priority scheduling occupation
  • id (most important)-idle CPU, to understand the percentage of idle CPU, mainly see the %id part
  • wa ——l/O waiting to be occupied
  • hi-hardware interrupt occupation
  • si-software interrupt occupation
  • st ——Virtualization occupancy

The fourth line of memory information:
Insert picture description here

  • total-total memory space
  • free-free space
  • used-used memory
  • buff/cache-the sum of the buffers of physical memory and swap memory

The fifth line of information exchange space:

Insert picture description here

  • total-total exchange space
  • free-free swap space
  • used-used swap memory
  • avail Mem —— Available physical space.
    Insert picture description here
    Explanation of each column in the process information area:
  • PID:
    process id
  • USER:
    the user name of the process owner
  • PR:
    Priority
  • NI:
    Modest degree value. Negative value means high priority, positive value means low priority
  • VIRT: The
    total amount of virtual memory used by the process, in kb
  • RES:
    physical memory size used by the process, in kb
  • SHR:
    shared memory size, unit kb
  • S:
    Process status
  • %CPU: The
    percentage of CPU time occupied from the last update to the present
  • %MEM: the
    percentage of physical memory used by the process
  • TIME+: The
    total CPU time used by the process, in units of 1/100 second
  • COMMAND:
    command name/command line

Top commonly used commands:

P键:根据CPU使用百分比大小进行排序
M键:根据驻留内存大小进行排序
N键:根据启动时间进行排序
c键:切换显示命令名称和完整命令行
h键:可以获得top程序的在线帮助信息
k键:根据提示输入指定进程的PID 号并按Enter 键终止对应的进程
q键:退出top程序
数字1键:显示CPU 个数和状态

1.5 pgrep command-view process information

Query process PID information based on specific conditions
(1) pgrep -l “log”

-l: display process name
, only PID number is output by default

Insert picture description here
pgrep -l -U root -t tty2

-U: Guiding feature users
-t: Designated terminal
Insert picture description here

1.6 pstree-view process tree

List process information in a tree structure
pstree -aup

-a:
display complete information
-u:
list the corresponding user name
-p:
list the corresponding PID number

Insert picture description here

2. How to start the process

2.1 Manual start

Start in the foreground: the
user enters the command and execute the program directly.
Start
in the background: add the "&" symbol at the end of the command line
Insert picture description here
(the output information includes the serial number of the background task and the PID number)

2.2 Schedule start

  • Use the at command to set up a one-time scheduled task
  • Use crontab command to set up periodic scheduled tasks

2.3 Fore and background scheduling of the process

  • Ctrl+Z key combination-suspend the current process, that is, transfer to the background and stop execution
  • jobs command
    -view the list of tasks in the background
    jobs [-l]
  • fg command
    -restore the background process to the foreground, you can specify the task number
    Insert picture description here

2.4 Terminate the operation of the process

2.4.1 Ctrl+C key combination

-Interrupt the command being executed

2.4.2 kill, killall commands

  • kill command-used to terminate the process with the specified PID number
  • killall command-used to terminate all processes related to the specified name
  • -9 option is used to force termination

2.4.3 pkill command

  • Terminate the corresponding process according to specific conditions
  • Common command options:
    -∪:
    Terminate the corresponding process according to the user name of the process
    -t: Terminate the corresponding process
    according to the terminal where the process is located

3. Plan task management

3.1 at command

One-time scheduled task:
at [00:00] [yyyy-mm-dd]
Insert picture description here

3.2 crontab command

Periodic scheduled tasks:

  • Repeat the user-specified command operation according to the preset time period (minutes, hours, days, months, weeks)
  • Periodic scheduled task
  • Main settings files:
    ——Global configuration files, located in the file: /etc/crontab ——System
    default settings, located in the directory: /etc/cron.*/ ——User
    -defined settings, located in the file: /var/spool/cron /username

Manage crontab scheduled tasks:

  • Edit the scheduled task
    crontab -e [-u username]

(-U defaults to the current user by default)

  • View the scheduled task
    crontab -4 [-u username]
  • Delete the scheduled task
    crontab -r [-u username]

The format of crontab task configuration:
Insert picture description here

Field Description
minute The value is any integer from 0 to 59
hour The value is any integer from 0 to 23
date The value is any integer from 1 to 31
month The value is any integer from 1 to 12
week The value is any integer from 0 to 7, 0 or 7 represents Sunday
command Command or program script to execute

Special representation method of time value:

* Indicates any time within the range For example, it means every day in the date position
Represents multiple discrete time points in an interval For example, 2 and 4 in the week position represent every Tuesday and Thursday
- Represents a continuous time range For example, 1-5 on the date position represents the 1st to the 5th
/ Specify the time frequency of the interval For example, de /4 in the hour position means every four hours

Example:

0 17 * * 1-5 Monday to Friday at 17:00
30 8 * * 1,3,5 Every Monday, Wednesday and Friday at 8:30
0 8-18/2 * * * Every two hours between 8 o'clock and 18 o'clock
0 * */3 * * Every three days

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/110387003