Shell command management process


  In addition to being a multi-user operating system, Linux is also a multi-tasking system. Multitasking means being able to run multiple programs at the same time. Linux provides tools to list running processes, monitor system usage, and stop (or kill) processes when necessary.

  Processes can be started, paused, stopped, or killed through a shell. In addition, processes can be placed in the background and brought to the foreground. This article details ps, top, kill, jobs, and other commands used to list and manage processes.

List processes

ps command

  ps(Process Status) is a commonly used Linux/Unix command used to view process information running in the current system. It can display the status of the process, resource usage, and other relevant information. psThe basic usage of the command is as follows:

ps [选项]

  Here are some commonly used pscommand options and examples:

  1. Display all processes of the current user:

ps

Insert image description here

  psPIDThe , TTY, TIMEand in the output of the command CMDare fields for information about the process. They represent the following contents respectively:

  · PID(Process ID): The unique identifier of the process, used to identify and manage the process. Each process has a unique PID, and the process can be operated and tracked through the PID.

  · TTY(Terminal Type): The terminal device associated with the process (if any). This field shows whether the process is associated with a terminal, and the associated terminal type. Normally, if the process is running on a terminal, the TTY field will display the terminal device name, otherwise it will display ?or pts/0etc.

  · TIME: The accumulated time that the process has occupied the CPU. It is usually displayed in hours, minutes, and seconds and is used to indicate how long a process has been running on the CPU.

  · CMD(Command): The command line to start the process. This field displays the complete command for the process, including the command name and possible arguments.

  2. Display detailed information of all processes, including the process's PID (process ID), PPID (parent process ID), CPU usage, memory usage, etc.:

ps aux

Insert image description here
  ps auxThe fields in the command's output represent the following:

  · USER: The user name that started the process. This is the owner of the process.

  · %CPU:CPU utilization occupied by the process, expressed in percentage.

  · %MEM: The physical memory utilization occupied by the process, expressed as a percentage.

  · VSZ(Virtual Memory Size): The virtual memory size of the process, in kilobytes (KB). It represents the total size of virtual memory currently used by the process.

  · RSS(Resident Set Size): The resident memory size of the process, in kilobytes (KB). It indicates the size of physical memory currently occupied by the process.

  · STAT: The status of the process. Usually represented by a single character, for example, Rindicating running, Sindicating hibernation, Zindicating zombie process, etc.

  · START: The time and date when the process was started.

  · COMMAND: The complete command line to start the process.

  3. Display all processes of the specified user, for example, display userall processes with the user name:

ps -u user

Insert image description here

  4. Display information about the specified process, for example, display 1234the process with PID:

ps -p 1234

Insert image description here

  5. Display processes in a tree structure, including parent-child relationships:

ps -ejH

Insert image description here

  · UID(User ID): The user ID that started the process. This is the numeric identifier of the owner of the process.

  · PID(Process ID): The unique identifier of the process, used to identify and manage the process.

  · PPID(Parent Process ID): The PID of the parent process, that is, the PID of the process that started the current process.

  · PGID(Process Group ID): The ID of the process group. Multiple processes can be organized into a process group.

  · SID(Session ID): The ID of the session. A session can contain multiple process groups.

  · C: The percentage of time slice occupied by the process.

  · STIME: The start time of the process.

top command

Insert image description here

  topThe command is a powerful tool for real-time monitoring of system performance. It will interactively display information about processes running in the system, including CPU usage, memory usage, process list, etc. To properly parse topthe output of the command, you can follow these steps:

  Understand the top summary: topThe top display of the command contains summary information about the system. The first line of the above figure is the system running time, number of users, and average system load.

  Average system load is a metric used to measure system load. It represents the number of processes running or waiting to be executed on the system over a period of time. Typically, the average system load is displayed as three values ​​corresponding to different time periods: 1 minute, 5 minutes, and 15 minutes.
To understand what these values ​​mean, you can interpret them as follows: First value (1-minute load average): It indicates on average how many processes were running or waiting to run in the past 1 minute. This value usually corresponds to the instantaneous load of the system. If this value is greater than the number of CPU cores in the system, it means that the system may be in an overloaded state. Second value (5-minute load average): It indicates how many processes were running or waiting to run on average over the past 5 minutes. This value usually corresponds to the load situation over a longer period of time and can be used to observe the load trend of the system. The third value (15-minute load average): It indicates how many processes were running or waiting to run on average over the past 15 minutes. This value usually corresponds to the load situation over a relatively long period of time and can also be used to observe the load trend of the system.   Normally, the average system load value should stay within the range of the number of CPU cores on the system (for example, if the system has 4 CPU cores, then the load should be between 1 and 4). If the average system load is consistently higher than the number of CPU cores on the system, it may indicate that the system is in an overloaded state and requires further analysis and processing, such as optimizing performance or adding hardware resources.
  
  
  

  The second line is task running statistics. The third line is CPU usage.

  us:用户空间(User Space)CPU 使用率。它表示在用户程序中消耗的CPU时间,例如运行应用程序。
  sy:系统空间(System Space)CPU 使用率。它表示在内核中执行系统调用或内核任务时消耗的CPU时间,例如处理硬件中断或文件系统操作。
  ni:Nice值(Nice)。Nice值是进程的调度优先级,如果有运行在较低优先级的进程,它们消耗的CPU时间会显示在这里。
  id:空闲(Idle)CPU 时间的百分比。这表示 CPU 空闲的时间百分比,没有任何任务在运行。
  wa:等待(Wait)CPU 时间的百分比。它表示 CPU 等待I/O操作完成的时间百分比,通常用于检测磁盘或其他I/O瓶颈。
  hi:硬中断(Hardware Interrupts)CPU 时间的百分比。它表示处理硬件中断的时间百分比,通常由硬件设备触发。
  si:软中断(Software Interrupts)CPU 时间的百分比。它表示处理软中断的时间百分比,通常由操作系统内核触发。
  st:虚拟化(Steal Time)CPU 时间的百分比。这个值通常在虚拟化环境中出现,表示虚拟机被宿主机夺走的CPU时间。

  第四行是系统内存和交换空间的统计信息。

  查看进程列表: top 的主要部分是进程列表,它会列出当前运行的所有进程。默认情况下,进程按照CPU使用率排序,最高的进程位于列表的顶部。你可以按下不同的键来改变排序方式,例如按下M键按内存使用率排序。

  理解各列的含义: 见本篇博客的ps命令介绍。

  交互式操作: top 命令允许在运行时进行交互式操作。你可以使用键盘上的不同键来改变排序、过滤进程、查看不同的系统信息等。按下h键可以查看帮助信息,列出了可用的交互命令。

  查看资源利用情况: 注意查看系统的资源利用情况,特别是 CPU 和内存的使用率。如果某个进程占用了大量资源,可以考虑采取措施来优化或终止该进程。

  退出 top 当完成分析后,可以按下q键来退出 top

管理后台进程

启动后台进程

  如果想要使某些程序在使用Shell的过程中始终运行,可以将它们放置到后台。为了将某一程序放置到后台,需要在命令行的末尾输入一个与号(&),如下所示:

java -jar a.jar &

  上面的代码指的是在后台执行java文件。

查看后台进程

  如果想要检查哪些命令在后台运行,可以使用jobs命令,如下所示:

jobs
[ 1] stopped (tty output) vi/tmp/myfile
[ 2] Running find /usr -print > /tmp/allusrfiles &
[ 3] Running nroff -man /usr/man2/* >/tmp/ man2 &
[ 4]- Running nroff -man /usr/man3/* >/tmp/man3 &
[ 5] + Stopped nroff -man /usr/man4/* >/tmp/man4

jobs和ps的区别

  jobsps 是两个不同的命令,用于查看系统中正在运行的进程和作业,它们有以下主要区别:

  1. 功能和用途

  • ps(Process Status):ps 命令用于查看系统中所有进程的信息,包括它们的PID、状态、CPU和内存使用情况等。它主要用于查看和管理进程。
  • jobsjobs 命令用于查看当前shell会话中的作业(通常是由shell启动的命令)。它主要用于查看和管理由当前shell会话启动的作业。

  2. 应用范围

  • ps 可以用于查看系统中所有进程,包括后台运行的系统进程和由其他用户启动的进程。

  • jobs 仅用于查看当前shell会话中的作业,通常用于查看和管理当前用户启动的命令。
      3. 输出格式

  • ps通常以详细的表格形式显示进程信息,提供更多的细节。

  • jobsDisplays a list of jobs in the current shell session, usually including job number, status, and command.
      4.Use : _

  • psUsually used for global system-level process monitoring, for system administrators or advanced users to view the processes in the system.

  • jobsUsed for job control in shell sessions, such as running jobs in the background, suspending jobs, resuming jobs, etc.

  For example, when you start a long-running command in the terminal and want it to run in the background, you can use the &symbol to put the command into the background, and then use jobsthe command to view the list of background jobs. The pscommand is used to view all processes running in the system, including background processes and processes of other users.

Stop process

kill -9 [进程pid]

Guess you like

Origin blog.csdn.net/qq_43592352/article/details/132777154