Linux进程管理和系统监控ps,top

在Linux下进程查看和进程管理是经常使用的命令,我们可以使用ps来查看上一个状态中,各进程的信息,也可以通过top命令来实时的查看进程的属性信息。可以通过kill来管理进程运行,通过调整renice值来调整进程的优先级。

而htop、dstat、glances则是非常优秀Linux的系统监控命令,接下来就一步一步介绍他们的使用。

  1. ps的使用

    查看ps是shell内置的命令还是外部命令

    [[email protected] ~]# type ps
    ps is /bin/ps

    由上可知ps是外部命令,外部命令基本都可以通过command --help查看命令的简短帮助信息

查看ps命令的详细使用,man ps

ps: 显示进程状态的命令,快照式、一次性
    ps - report a snapshot of the current processes.
SYNOPSIS
    ps [options]
DESCRIPTION
    ps displays information about a selection of the active processes. If you want a

repetitive(重复)update of the selection and the displayed information, use top(1) instead.
    支持两种风格:SysV, BSD
This version of ps accepts several kinds of options:
 1   UNIX options, which may be grouped and must be preceded by a dash.
 UNIX的选项,这可能是分组之前,必须用一个破折号。
 2   BSD options, which may be grouped and must not be used with a dash.
 BSD选项,这可能是分组,不得使用破折号。
 3   GNU long options, which are preceded by two dashes.

ps命令把进程分为两类:
        与终端相关的进程: a   ps a

a   Lift the BSD-style "only yourself" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes with a terminal (tty), or to list all processes when used together with the x option.

与终端无关的进程:x   ps x

x   Lift the BSD-style "must have a tty" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the ps   personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the a option.

     用户为中心显示状态信息:u

u  display user-oriented format

常用组合:ps aux

USER  那个用户启动的     
PID   进程号

pid        PID      process ID number of the process.

%CPU  cpu占用百分比

%cpu       %CPU     cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed  as a percentage. It will not add up to 100% unless you are lucky. (alias pcpu).

%MEM  内存占用百分比

%mem       %MEM     ratio of the process’s resident set size  to the physical memory on the machine,expressed as a percentage. (alias pmem).

VSZ: Virtual memory SiZe 线性地址空间所占用的大小

vsz        VSZ      virtual memory size of the process in KiB (1024-byte units). Device mappings are currently excluded; this is subject to change. (alias vsize).

RSS: 常驻内存集  使用过程中不能交换出去的内存空间

rss        RSS      resident set size, the non-swapped physical memory that a task has used
(in kiloBytes). (alias rssize, rsz).

TTY  跟哪个终端相关
STAT状态:  进程的状态
   R: running  运行态

R    Running or runnable (on run queue)

S: 可中断睡眠

S    Interruptible sleep (waiting for an event to complete)

D:不可中断睡眠

D    Uninterruptible sleep (usually IO)

T:stopped 停止态

T    Stopped, either by a job control signal or because it is being traced.

Z:zombie  僵死态

Z    Defunct ("zombie") process, terminated but not reaped by its parent.

s: session leader 会话的领导者

s    is a session leader

+:前台进程

+    is in the foreground process group

l: 多线程进程

l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)

N:低优先级进程

N    low-priority (nice to other users)

<: 高优先级进程

<    high-priority (not nice to other users)

COMMAND:包含在方括号中的进程表示为内核线程

[kthreadd]  加了[]说明是内核线程
常用组合:ps -ef
   -e: 显示所有进程

-e    Select all processes. Identical to -A.

-f: 显示完整格式的信息

-f     does full-format listing. This option can be combined with many other UNIX-style
options to add additional columns. It also causes the command arguments to be printed. When used with -L, the NLWP (number of threads) and LWP (thread ID)columns will be added. See the c option, the format keyword args, and the format keyword comm.

ppid       PPID     parent process ID.  父进程的id.

c          C        processor utilization. Currently, this is the integer value of the percent usage over the lifetime of the process. (see %cpu).

C     pcpu         cpu utilization   cpu使用率

常用组合:ps -eFH
   -F: 显示额外信息

-F    extra full format. See the -f option, which -F implies.

-H:显示进程的层次结构

-H              show process hierarchy (forest)

psr        PSR      processor that process is currently assigned to.在哪个cpu上运行,本实验环境是有2个cpu总共8核。

自定义要显示的信息:o

o format    specify user-defined format. Identical to -o and --format.

ps axo pid,command,psr,pri,ni,..   需要注意o需要写在后面

ni: nice值
       pri: 优优级
       psr: 运行的CPU

显示进程号(PID)和父进程号(PPID)运行的CPU(PRI)和优先级(PRI)

2.top的使用

top:   top可以动态的显示

top - display Linux tasks
显示Linux工作任务
SYNOPSIS
       top -hv | -abcHimMsS -d delay -n iterations -p pid [, pid ...]

The traditional switches ’-’ and whitespace are optional.

DESCRIPTION
       The  top  program  provides  a dynamic real-time view of a running system.  It can display system summary information as well as a list of tasks currently being managed by the Linux kernel.   The types  of system summary information shown and the types, order and size of information displayed for tasks are all user  configurable  and that configuration  can  be  made  persistent  across restarts.

       
 us: user space
 sy: system
 ni: nice值调整所占用时间百分比
 id: 空闲百分比
 wa: wait io cpu用于等待io事件完成所消耗的时间占据的百分比
 hi: hardware interrupt
 si: soft interrupt
 st: 被虚拟机拿走的时间
 2c. SUMMARY Area Fields
 The summary area fields describing CPU statistics are abbreviated.They provide
 摘要区域字段描述CPU统计数据是缩写。他们提供所花时间信息:

information about times spent in:
 us = user mode
 sy = system mode
 ni = low priority user mode (nice)
 id = idle task
 wa = I/O waiting
 hi = servicing IRQs
 si = servicing soft IRQs
 st = steal (time given to other DomU instances)
VIRT  虚拟内存集
RES  常驻内存集
SHR  共享内存大小
S   进程状态

输入字符
      M: 内存百分比

P: CPU百分比

T: 累积占用的CPU时间

l: 显示或不显示负载信息
        过去1分钟、5分钟、15分钟的平均负载
        load average: 0.00, 0.00, 0.00


      t: 显示或不显示进程及CPU相关的信息

        
      1: 数字,分别显示各CPU的相关信息

m: 显示或不显示物理内存和交换内存的相关信息         
      q: 退出
      k: 终止指定进程
      s: 修改刷新时间间隔

后面这几个操作可以自己输入后,查看显示信息的变化,注意按对应字符后,再按一次又返回到原来的显示

常用选项:
   -d #: 指定刷新时间间隔   指定刷新时间后,刷新次数就可以调整了
   -b: 以批次的方式显示top的刷新,会把所有的任务都显示出来,只是是翻转的方式  只能按Ctrl+c退出
   -n #: 显示的批次  显示#次后会自动退出

这些选项可以自己来实验显示

kill使用

[[email protected] ~]# kill -l
 1) SIGHUP     2) SIGINT     3) SIGQUIT     4) SIGILL     5) SIGTRAP
 6) SIGABRT     7) SIGBUS     8) SIGFPE     9) SIGKILL    10) SIGUSR1
11) SIGSEGV    12) SIGUSR2    13) SIGPIPE    14) SIGALRM    15) SIGTERM
16) SIGSTKFLT    17) SIGCHLD    18) SIGCONT    19) SIGSTOP    20) SIGTSTP
21) SIGTTIN    22) SIGTTOU    23) SIGURG    24) SIGXCPU    25) SIGXFSZ
26) SIGVTALRM    27) SIGPROF    28) SIGWINCH    29) SIGIO    30) SIGPWR
31) SIGSYS    34) SIGRTMIN    35) SIGRTMIN+1    36) SIGRTMIN+2    37) SIGRTMIN+3
38) SIGRTMIN+4    39) SIGRTMIN+5    40) SIGRTMIN+6    41) SIGRTMIN+7    42) SIGRTMIN+8
43) SIGRTMIN+9    44) SIGRTMIN+10    45) SIGRTMIN+11    46) SIGRTMIN+12    47) SIGRTMIN+13
48) SIGRTMIN+14    49) SIGRTMIN+15    50) SIGRTMAX-14    51) SIGRTMAX-13    52) SIGRTMAX-12
53) SIGRTMAX-11    54) SIGRTMAX-10    55) SIGRTMAX-9    56) SIGRTMAX-8    57) SIGRTMAX-7
58) SIGRTMAX-6    59) SIGRTMAX-5    60) SIGRTMAX-4    61) SIGRTMAX-3    62) SIGRTMAX-2
63) SIGRTMAX-1    64) SIGRTMAX   
[[email protected] ~]# kill -19 13065

通过renice来改变,运行进程的优先级

renice - alter priority of running processes

SYNOPSIS
     renice [-n] priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]
     renice -h | -v

DESCRIPTION
     Renice alters the scheduling priority of one or more running processes.  The following who parame-ters are interpreted as process ID’s, process group ID’s, or user names.

[[email protected] ~]# renice -10 -p 13037
13037: old priority 0, new priority -10

时间: 2024-08-09 19:54:06

Linux进程管理和系统监控ps,top的相关文章

【linux之进程管理,系统监控】

一.进程管理 前台进程:一般是指占据着标准输入和/或标准输出的进程后台进程:不占据默认开启的进程都是前台进程ctrl+C 中断ctrl+z 从前台转入后台bg 后台进程编号 让其在后台运行ls -R / & 让其在后台进行 使一个进程在后台进行1.ctrl+z将前台进程调入后台,默认进行到后台后处于停止状态 若想要在后台运行该后台进程,使用 bg # (#表示后台进程编号),此时ctrl+c不管用2.在开启进程的时候,使用&符号缀在命令之后,也是在后台运行该进程 使用fg命令可以将已被调入

Linux 进程管理命令ps、top、kill、nice、fg、bg、vmstat、uptime

Linux 进程管理命令: ps process status 查看进行相关信息: system 风格:sysv  有-线引导 BSD风格:BSD ps a:显示所有跟终端有关的进程 u:显示更详细的字段 x:显示所有跟终端无关的进程 常用 ps ax 进程分类: 终端启动(相关)的进程:用户通过命令行启动的进程 跟终端无关的进程:系统启动就自己启动 进程状态: D: 表示不可中断的睡眠 R:表示就绪态 S:表示可中断的睡眠 T:停止 Z:僵死态 <:高优先级进程 N:低优先级进程 +:前台进程

Linux进程管理四大工具ps \dstat\ top\ htop

Linux进程管理之“四大名捕” 一.四大名捕 四大名捕,最初出现于温瑞安创作的武侠小说,是朝廷中正义力量诸葛小花的四大徒弟,四人各怀绝技,分别是轻功暗器高手“无情”.内功卓越的高手“铁手”.腿功惊人的“追命”和剑法一流的“冷血”本文四大名捕由linux命令所出演      无情:ps     出演      铁手:dstat  出演      追命:top    出演      冷血:htop   出演 二.进程相关基础知识 介绍四大名捕之前先介绍一下进程相关的基础知识,话不多说,看图. 三.

【linux_笔记】Linux进程管理(ps)

学习资源来自:www.magedu.com 学习记录过程难免出现错误,示例仅供参考,大神请无视.. 进程管理相关命令:    ps:Process State(进程状态)        SysV风格: -elF            常用命令格式:                ps -e                    ps -el | head                 ps -elF | head                ps -eF    (常用)          

Linux进程管理之ps的使用

主题Linux进程管理之ps工具的使用 一ps工具的介绍 ps: process state  进程状态ps - report a snapshot of the current processesLinux系统各进程的相关信息均保存在/proc/PID目录下的各文件中 默认显示的内容很少 [[email protected] ~]# ps PID TTY TIME CMD 2018 pts/0 00:00:00 bash 2656 pts/0 00:00:00 ps [[email prote

Linux进程管理简谈

Linux系统进程管理简谈 进程是什么? 简单来说进程是一个正在执行的程序的一个副本,存在生命周期,有段指令和代码在不断运行. linux内核存储信息的固定格式:task struct 进程的相关信息存储在链表中 多个任务的task struct组件的链表:task list 进程的创建:父进程创建子进程(内核创建init进程,剩余一切进程有init及其子进程进程创建) 父进程创建子进程时向内核调用fork()来创建子进程并且通过调用clone()复制父进程的信息给子进程 Linux进程的优先级

linux进程管理(四)

[教程主题]:进程管理 [1]进程介绍 程序和进程 程序是为了完成某种任务而设计的软件,比如OpenOffice是程序.什么是进程呢?进程就是运行中的程序. 一个运行着的程序,可能有多个进程. 比如自学it网所用的WWW服务器是apache服务器,当管理员启动服务后,可能会有好多人来访问,也就是说许多用户来同时请求httpd服务,apache服务器将会创建有多个httpd进程来对其进行服务. 进程分类: 进程一般分为交互进程.批处理进程和守护进程三类. 值得一提的是守护进程总是活跃的,一般是后台

Linux进程管理.md

目录 Linux进程管理 1.进程基本概述 1.1.进程的组成部分 1.2.进程的环境 1.3.进程的产生 1.4.进程的分类 2.进程状态 2.1.Linux进程状态 3.2.进程优先级 3.3.相对优先级 3.4.nice级别与权限 3.5.进程优先级调整 4.进程管理命令 4.1.ps 4.2.pstree 4.3.pgrep 4.4.pidof 4.5.vmstat 5.控制作业 5.1.作业与会话 5.2.作业分类 5.3.在后台运行作业 6.进程间通信 7.使用信号控制进程 8.监控

linux进程管理命令总结

linux进程管理的相关命令: 1.pstree  --以树状形式显示当前系统进程,和命令tree相似. 2.ps命令 命令格式:ps [options] 常用组合:aux: u:以用户为中心组织的进程状态信息显示 a:与终端相关的进程 x:与终端无关的进程 #ps aux USER : 用户名 PID : 进程号 %CPU :占用的cpu百分比 %MEM  :内存的使用率 VSZ :  virtual memory size ,虚拟内存集 RSS : resident size ,常驻内存集