zoukankan      html  css  js  c++  java
  • linux ps命令

    语法注意:

    ps(process status)命令带有2种不一样的风格,分别是BSD和UNIX。新用户经常会混淆和错误地解释这两种风格。所以要弄清楚他们,继续操作之前这里是一些基本的信息。

    注意:"ps aux"和"ps -aux"不相同。例如"-u"用来显示该用户的进程。但是"u"则是显示详细的信息。

    BSD风格:在BSD风格的语法选项前不带连字符。

    1. ps aux 

    UNIX/LINUX的风格:在linux风格的语法选项前面有一个破折号如常。…

    1. ps -ef 

    混合使用两种Linux系统上的语法风格是好事儿。例如“ps ax -f”。但在这篇文章中,我们将主要集中在UNIX风格的语法。

    如何使用ps命令呢?

    1、显示所有进程:

    下面的命令将列出所有的进程:

    1. $ ps ax 
    2. $ ps -ef 

    加上管道输出给less,来滚动显示

    "u"或者"-f"参数来显示所有进程的详细信息

    1. $ ps aux 
    2. $ ps -ef -f 

    注意:为什么用户列不显示我的用户名,但显示其他用户,如root、www等,对于所有的用户名(包括你)如果长度大于8个字符,然后ps将只显示UID,而不是用户名。

    3、通过名字和进程ID显示进程:

    通过名字或命令搜索进程,使用“-C”选项后面加搜索词。

    1. $ ps -C apache2 
    2.   PID TTY          TIME CMD 
    3.  2359 ?        00:00:00 apache2 
    4.  4524 ?        00:00:00 apache2 
    5.  4525 ?        00:00:00 apache2 

    ------------------------

    要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而 ps 命令就是最基本同时也是非常强大的进程查看命令。使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等。总之大部分信息都是可以通过执行该命令得到的。

    ps 为我们提供了进程的一次性的查看,它所提供的查看结果并不动态连续的;如果想对进程时间监控,应该用 top 工具

    kill 命令用于杀死进程。

    linux上进程有5种状态: 

    1. 运行(正在运行或在运行队列中等待) 

    2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 

    3. 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生) 

    4. 僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放) 

    5. 停止(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行运行) 

    ps工具标识进程的5种状态码: 

    D 不可中断 uninterruptible sleep (usually IO) 

    R 运行 runnable (on run queue) 

    S 中断 sleeping 

    T 停止 traced or stopped 

    Z 僵死 a defunct (”zombie”) process 

    ps 与grep 常用组合用法,查找特定进程

    命令:

    ps -ef|grep ssh

    Terms

    • VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
    • RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存)
    • PSS- Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
    • USS- Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)

    一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS

    Overview

    The aim of this post is to provide information that will assist in interpreting memory reports from various tools so the true memory usage for Linux processes and the system can be determined.

    Android has a tool called procrank (/system/xbin/procrank), which lists out the memory usage of Linux processes in order from highest to lowest usage. The sizes reported per process are VSS, RSS, PSS, and USS.

    For the sake of simplicity in this description, memory will be expressed in terms of pages, rather than bytes. Linux systems like ours manage memory in 4096 byte pages at the lowest level.

    VSS (reported as VSZ from ps) is the total accessible address space of a process.This size also includes memory that may not be resident in RAM like mallocs that have been allocated but not written to. VSS is of very little use for determing real memory usage of a process.

    RSS is the total memory actually held in RAM for a process.RSS can be misleading, because it reports the total all of the shared libraries that the process uses, even though a shared library is only loaded into memory once regardless of how many processes use it. RSS is not an accurate representation of the memory usage for a single process.

    PSS differs from RSS in that it reports the proportional size of its shared libraries, i.e. if three processes all use a shared library that has 30 pages, that library will only contribute 10 pages to the PSS that is reported for each of the three processes. PSS is a very useful number because when the PSS for all processes in the system are summed together, that is a good representation for the total memory usage in the system. When a process is killed, the shared libraries that contributed to its PSS will be proportionally distributed to the PSS totals for the remaining processes still using that library. In this way PSS can be slightly misleading, because when a process is killed, PSS does not accurately represent the memory returned to the overall system.

    USS is the total private memory for a process, i.e. that memory that is completely unique to that process.USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaksin a process.

    For systems that have Python available, there is also a nice tool calledsmem that will report memory statistics including all of these categories.

    ps命令查出来的rss/vsz/size分别是何含义呢

    rss RSS resident set size, the non-swapped physical memory that a task has used (in kiloBytes). (alias rssize, rsz).
    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).
    size SZ approximate amount of swap space that would be required if the process were to dirty all writable pages and then be
      swapped out. This number is very rough!
    sz SZ size in physical pages of the core image of the process. This includes text, data, and stack space. Device mappings
      are currently excluded; this is subject to change. See vsz and rss.
    SIZE: 进程使用的地址空间, 如果进程映射了100M的内存, 进程的地址空间将报告为100M内存. 事实上, 这个大小不是一个程序实际使用的内存数.
    RSS: "Resident Set Size", 实际驻留"在内存中"的内存数. 不包括已经交换出去的代码. 举一个例子: 如果你有一个程序使用了100K内存, 操作系统交换出40K内存, 那么RSS为60K. RSS还包括了与其它进程共享的内存区域. 这些区域通常用于libc库等.
    SHARE: RSS中与其它进程共享的内存部分大小.
    VMSIZE: 一个进程占用的总的地址空间大小. 它包括了没有映射到内存中的页面。
    sz(Private RSS): 映射到内存中的页面, 这些页面仅由进程单独使用. 这也是我们最关心地方: 进程实际占用的内存数。
     
  • 相关阅读:
    同一根域名的多站点登录共享
    mysql忘记管理员密码
    使用Cacti监控你的网络(四) Cacti脚本及模板
    使用Cacti时常见的问题集
    SQL Server:SQL Like 通配符特殊用法:Escape
    IS6.0 应用程序池Web园导致Session丢失
    VMware建立虚拟机无法上网
    SqlServer 添加用户 添加角色 分配权限
    教你如何在SQL Server数据库中加密数据
    sendmail邮件服务器搭载smtp和pop3认证的配置方法
  • 原文地址:https://www.cnblogs.com/youxin/p/4019909.html
Copyright © 2011-2022 走看看