编程小贴士

给你的编程提供小点子


12个进程管理命令介绍

执行中的程序在称作进程。当程序以可执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源、内存、安全属性和与之相关的状态。可以有多个进程关联到同一个程序,并同时执行不会互相干扰。操作系统会有效地管理和追踪所有运行着的进程。

为了管理这些进程,用户应该能够:

  • 查看所有运行中的进程
  • 查看进程消耗资源
  • 定位个别进程并且对其执行指定操作
  • 改变进程的优先级
  • 杀死指定进程
  • 限制进程可用的系统资源等

Linux提供了许多命令来让用户来高效掌控上述的操作。接下来,一个一个的来讲解下。

tips1000_2015-06-14_05-54-5

1. ps

‘ps’是Linux 中最基础的浏览系统中的进程的命令。能列出系统中运行的进程,包括进程号、命令、CPU使用量、内存使用量等。下述选项可以得到更多有用的消息。

ps -a - 列出所有运行中/激活进程

tips1000_2015-06-14_05-54-5

ps -ef |grep - 列出需要进程
ps -aux - 显示进程信息,包括无终端的(x)和针对用户(u)的进程:如USER, PID, %CPU, %MEM等

2. pstree

linux中,每一个进程都是由其父进程创建的。此命令以可视化方式显示进程,通过显示进程的树状图来展示进程间关系。如果指定了pid了,那么树的根是该pid,不然将会是init(pid: 1)。

tips2000_2025-06-24_05-54-54

3. top

‘top’是一个更加有用的命令,可以监视系统中不同的进程所使用的资源。它提供实时的系统状态信息。显示进程的数据包括 PID、进程属主、优先级、%CPU、%memory等。可以使用这些显示指示出资源使用量。

tips1000_2015-06-14_05-54-53

4. htop

htop与top很类似,但是htop是交互式的文本模式的进程查看器。它通过文字图形化地显示每一个进程的CPU和内存使用量、swap使用量。使用上下光标键选择进程,F7和F8改变优先级,F9杀死进程。Htop不是系统默认安装的,所以需要额外安装。

tips2000_2025-06-24_05-54-53

5. nice

通过nice命令的帮助,用户可以设置和改变进程的优先级。提高一个进程的优先级,内核会分配更多CPU时间片给这个进程。默认情况下,进程以0的优先级启动。进程优先级可以通过top命令显示的NI(nice value)列查看。

进程优先级值的范围从-20到19。值越低,优先级越高。

nice <优先值> <进程名> - 通过给定的优先值启动一个程序

tips1000_2015-06-14_05-54-54

上述命令例子中,可以看到‘top’命令获得了-3的优先值。

6. renice

renice命令类似nice命令。使用这个命令可以改变正在运行的进程优先值。注意,用户只能改变属于他们自己的进程的优先值。

renice -n -p - 改变指定进程的优先值

初始优先值为0的3806号进程优先值已经变成了4.

renice -u -g - 通过指定用户和组来改变进程优先值

上述例子中,用户为‘mint’的所有进程优先值变为‘-3’。

7. kill

这 个命令用于发送信号来结束进程。如果一个进程没有响应杀死命令,这也许就需要强制杀死,使用-9参数来执行。注意,使用强制杀死的时候一定要小心,因为进 程没有时机清理现场,也许写入文件没有完成。如果我们不知道进程PID或者打算用名字杀死进程时候,killall就能派上用场。

kill <pid>
kill -9 <pid>
killall -9 - 杀死所有拥有同样名字的进程

如果你使用kill,你需要知道进程ID号。pkill是类似的命令,但使用模式匹配,如进程名,进程拥有者等。

pkill <进程名>

tips4000_4045-06-44_05-54-54

8. ulimit

该命令用于控制系统资源在shell和进程上的分配量。对于系统管理员是最有用的,可以管理重度使用和存在性能问题的系统。限制资源大小可以确保重要进程持续运行,其他进程不会占用过多资源。

ulimit -a - 显示当前用户关联的资源限制

tips3000_3035-06-34_05-54-54

-f - 最大文件尺寸大小
-v - 最大虚拟内存大小(KB)
-n - 增加最大文件描述符数量
-H : 改变和报告硬限制
-S : 改变和报告软限制

浏览ulimit man页面获取更多选项。

9. w

w 提供当前登录的用户及其正在执行的进程的信息。显示信息头包含信息,如当前时间、系统运行时长、登录用户总数、过去的1,5,15分钟内的负载均衡数。

基于这些用户信息,用户在终止不属于他们的进程时要小心。

tips1000_2015-06-14_05-54-55

who是类似命令,提供当前登录用户列表、系统启动时间、运行级别等。

tips1000_2015-06-14_05-54-5

whoami命令输出当前用户ID

10. pgrep

pgrep的意思是”进程号全局正则匹配输出”。该命令扫描当前运行进程,然后按照命令匹配条件列出匹配结果到标准输出。对于通过名字检索进程号是很有用。

pgrep -u mint sh

这个命令将会显示用户为‘mint’和进程名为‘sh’的进程ID。

tips2000_2025-06-24_05-54-55

11. fg , bg

有时,命令需要很长的时间才能执行完成。对于这种情况,我们使用‘bg’命令可以将任务放在后台执行,而用‘fg’可以调到前台来使用。

我们可以通过‘&’在后台启动一个程序:

find . -name *iso > /tmp/res.txt &

一个正在运行的程序也可以通过“CTRL+Z”和“bg”命令组合放到后台运行。

find . -name *iso > /tmp/res.txt &     -  启动一个程序
ctrl+z      -   挂起当前执行程序
bg   -  将程序放到后台运行

我们可以使用‘jobs’命令列出所有后台进程。

jobs

使用‘fg’命令可以将后台程序调到前台执行。

fg %进程id

tips4000_4045-06-44_05-54-55

12. ipcs

ipcs命令报告进程间通信设施状态。(共享内存,信号量和消息队列)

用-p参数联合-m、-s或-q使用,可以获得相关的进程间通信的进程ID。

ipcs -p -m

下面屏幕截图列出了最近访问了共享内存段的进程的创建者的ID和进程ID。

tips3000_3035-06-34_05-54-55

总结

总之 ,这些命令可以帮助管理员修复问题和改善性能。同样作为一名普通用户也需要解决进程出现的问题。所以,熟悉如此繁多的命令,从能有效管理进程是行之有效。

作者:B N Poornima译者:VicYu/Vic020校对:wxy

本文由 LCTT原创翻译,Linux中国荣誉推出

本文由 LCTT 原创翻译,Linux中国首发。也想加入译者行列,为开源做一些自己的贡献么?欢迎加入LCTT!

翻译工作和译文发表仅用于学习和交流目的,翻译工作遵照CC 协议规定,如果我们的工作有侵犯到您的权益,请及时联系我们。

115 Responses to “ 12个进程管理命令介绍 ”

  1. see pron说道:

    jH2Dfs This is the worst post of all, IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžve study

  2. Keep up the wonderful work , I read few articles on this site and I believe that your weblog is rattling interesting and contains sets of great info.

  3. Tattoo Artists说道:

    Very good blog! Do you have any tips and hints for aspiring writers?

  4. Very nice post. I just stumbled upon your weblog and wished to say that I ave really enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again soon!

  5. girl drawing说道:

    Thanks again for the blog post.Really thank you! Want more.

  6. I used to be recommended this blog by way of my cousin.

  7. Lung Health说道:

    Looking forward to reading more. Great blog post.Thanks Again. Will read on

  8. This is one awesome article.Much thanks again. Really Cool.

  9. My Face Gifts说道:

    It as really a great and helpful piece of info. I am glad that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

  10. Your blog is one of a kind, i love the way you organize the topics.: a-аАа’аАТ‚аЂТ˜

  11. This particular blog is no doubt educating and besides factual. I have picked up a bunch of interesting stuff out of it. I ad love to return every once in a while. Thanks!

  12. There is obviously a lot to identify about this. I feel you made some nice points in features also.

  13. This is a very good thing, is your best choice, this is a good thing.

  14. Oluwadamilare说道:

    Perfectly written content, Really enjoyed studying.

  15. magnificent issues altogether, you just received a brand new reader. What would you recommend about your submit that you simply made a few days ago? Any sure?

  16. Man I love your posts, just can at stop reading. what do you think about some coffee?

  17. Wow, awesome blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is great, as well as the content!

  18. visit说道:

    Really appreciate you sharing this article post.Much thanks again. Awesome.

  19. Really appreciate you sharing this blog article. Really Cool.

  20. webpage说道:

    things or advice. Maybe you could write next articles referring to this article.

  21. w88 world说道:

    Looking forward to reading more. Great post.Really thank you! Awesome.

  22. So happy to have located this submit.. Is not it wonderful any time you come across a fantastic submit? Enjoying the post.. appreciate it Fantastic thoughts you ave got here..

  23. click here说道:

    Wow, fantastic weblog structure! How long have you been running a blog for? you made blogging glance easy. The entire look of your website is excellent, let alone the content!

  24. hotel websites说道:

    Merely wanna comment that you have a very decent web site, I enjoy the design it really stands out.

  25. pre booked说道:

    I think other site proprietors should take this web site as an model, very clean and wonderful user genial style and design, as well as the content. You are an expert in this topic!

  26. Yahoo results While browsing Yahoo I discovered this page in the results and I didn at think it fit

  27. wow, awesome blog article.Really thank you! Cool.

  28. microblog说道:

    Really informative article post.Much thanks again.

  29. for more info说道:

    This is exactly what I was looking for, many thanks

  30. click here说道:

    Website We Recommend You made some fine points there. I did a search on the subject and found nearly all folks will consent with your blog.

  31. IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžve learn a few excellent stuff here. Definitely price bookmarking for revisiting. I wonder how so much attempt you put to make this kind of great informative web site.

  32. There may be noticeably a bundle to find out about this. I assume you made sure good points in features also.

  33. Wonderful article! We are linking to this great content on our website. Keep up the great writing.

  34. It as hard to come by knowledgeable people on this subject, however, you sound like you know what you are talking about! Thanks

  35. Very interesting information!Perfect just what I was looking for! Peace, commerce and honest friendship with all nations entangling alliances with none. by Thomas Jefferson.

  36. anchor说道:

    It as not that I want to copy your web page, but I really like the style and design. Could you let me know which design are you using? Or was it tailor made?

  37. Nwokolo说道:

    There is noticeably a bundle to understand about this. I assume you may have created specific nice points in functions also.

  38. stupefaction goombay murdstone Concetta breese veruca husk camembert tot

  39. It as good site, It as good site, I was looking for something like this

  40. Very good post.Thanks Again. Really Great.

  41. craft supplies说道:

    Your style is unique in comparison to other people I ave read stuff from. Thank you for posting when you have the opportunity, Guess I will just bookmark this site.

  42. I really liked your article.Thanks Again. Fantastic.

  43. Really appreciate you sharing this post. Really Cool.

  44. Say, you got a nice blog.Thanks Again. Fantastic.

  45. Some genuinely choice articles on this website , saved to bookmarks.

  46. Tattoo Artists说道:

    Thanks so much for the blog article.Really looking forward to read more. Will read on

  47. yeah bookmaking this wasn at a speculative determination outstanding post!.

  48. You made some good points there. I did a search on the topic and found most people will go along with with your blog.

  49. This very blog is really awesome and also amusing. I have found a lot of interesting things out of this amazing blog. I ad love to go back again soon. Cheers!

  50. url shortener说道:

    You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this web site.

  51. fitness说道:

    please visit the internet sites we follow, which includes this one particular, because it represents our picks from the web

  52. usually posts some very exciting stuff like this. If you are new to this site

  53. Thank you for all of the effort on this blog

  54. Wonderful blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I ave been trying for a while but I never seem to get there! Thanks

  55. I rruky epprwcierwd your own podr errickw.

  56. This unique blog is really awesome and also diverting. I have discovered many useful things out of it. I ad love to visit it every once in a while. Thanks a lot!

  57. Your style is unique compared to other people I ave read stuff from. Many thanks for posting when you ave got the opportunity, Guess I will just book mark this page.

  58. Thanks for the blog post.Much thanks again. Really Great.

  59. my page说道:

    Thanks for sharing, this is a fantastic post.Really thank you! Really Cool.

  60. white shoes说道:

    Looking forward to reading more. Great article. Want more.

  61. worth说道:

    I really love your site.. Very nice colors & theme. Did you make this site yourself? Please reply back

  62. refinances could be a great method to ramp up a new financial plan.

  63. School news说道:

    Very good article post.Much thanks again. Much obliged.

  64. Exams说道:

    I value the article post.Really looking forward to read more. Want more.

  65. ark ps4 server说道:

    Thanks for the article post.Thanks Again. Keep writing.

  66. Thank you for your post.Thanks Again. Want more.

  67. very good put up, i definitely love this website, carry on it

  68. Major thanks for the article.Really thank you! Want more.

  69. Incredibly Interesting Blog site! Thank you Just for this Website!

  70. Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is wonderful, as well as the content!. Thanks For Your article about sex.

  71. Major thanks for the post.Thanks Again. Will read on

  72. Your style is very unique in comparison to other people I ave read stuff from. I appreciate you for posting when you have the opportunity, Guess I all just bookmark this web site.

  73. Superb post here, thought I could learn more from but we can learn more from this post.

  74. tututorials说道:

    Thank you, I ave recently been searching for info about this topic for ages and yours is the best I ave discovered till now. But, what about the bottom line? Are you sure about the source?

  75. Your style is so unique in comparison to other folks I ave read stuff from. Many thanks for posting when you ave got the opportunity, Guess I will just book mark this blog.

  76. Some really good information, Sword lily I discovered this. What you do speaks therefore loudly that i cannot hear that which you say. by Ron Waldo Emerson.

  77. Open Heaven说道:

    Normally I do not read article on blogs, but I wish to say that this write-up very forced me to take a look at and do it! Your writing taste has been surprised me. Thank you, quite great article.

  78. My brother recommended I might like this website. He was entirely right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

  79. Wow, great blog article.Really looking forward to read more. Awesome.

  80. That is a good tip especially to those fresh to the blogosphere. Brief but very accurate information Many thanks for sharing this one. A must read article!

  81. Way cool! Some extremely valid points! I appreciate you penning this post plus the rest of the site is also really good.

  82. Thanks for one as marvelous posting! I quite enjoyed reading it,

  83. Online Article Every so often in a while we choose blogs that we read. Listed underneath are the latest sites that we choose

  84. Billy graham说道:

    It as hard to find well-informed people in this particular subject, but you sound like you know what you are talking about! Thanks

  85. view说道:

    This is a good tip particularly to those new to the blogosphere. Short but very accurate info Thank you for sharing this one. A must read article!

  86. Wow, great article post.Much thanks again. Great.

  87. more info说道:

    It as difficult to find educated people about this topic, but you sound like you know what you are talking about! Thanks

  88. more info说道:

    Paragraph writing is also a fun, if you be acquainted with afterward you can write or else it is complicated to write.

  89. visit website说道:

    Sweet blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I ave been trying for a while but I never seem to get there! Appreciate it|

  90. Thanks for the article post.Thanks Again. Really Great.

  91. Very informative blog article.Really thank you! Fantastic.

  92. Say, you got a nice blog article.Much thanks again. Want more.

  93. Spot on with this write-up, I honestly believe this amazing site needs much more attention. I all probably be returning to see more, thanks for the information!

  94. Share posts说道:

    Utterly pent articles , regards for selective information.

  95. for more info说道:

    you could have a fantastic weblog right here! would you prefer to make some invite posts on my weblog?

  96. you have a terrific weblog here! would you like to make some invite posts on my weblog?

  97. Free ads说道:

    Someone essentially help to make significantly posts I’d

  98. Thanks for sharing this good piece. Very inspiring! (as always, btw)

  99. I think, that you are not right. I am assured. I can prove it. Write to me in PM, we will discuss.

  100. Thank you for your blog.Thanks Again. Really Great.

  101. Andrea Roome说道:

    fashion jewelries will be one of the best stuffs that you can use to enhance your personal style’

  102. Sorry for my English.Great blog here! Also your site loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol.

  103. Agen Joker123说道:

    Never Ignore The significance Of Extras Like Speaker systems

  104. You are so cool! I don at suppose I ave read through a single thing like this

  105. You made some decent points there. I looked on the internet for the subject and found most individuals will agree with your blog.

  106. my blog说道:

    wow, awesome blog post.Thanks Again. Will read on

  107. recommend to my friends. I am confident they will be benefited from this website.

  108. Witty! I am bookmarking you site for future use.

  109. click here说道:

    What as up, I read your new stuff regularly. Your writing style is witty, keep it up!

  110. good day说道:

    Thanks for sharing, this is a fantastic blog article.Thanks Again. Will read on

  111. My brother recommended I might like this web site. He was entirely right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

  112. I’а†ve learn a few just right stuff here. Certainly price bookmarking for revisiting. I surprise how so much effort you place to make this type of magnificent informative site.

  113. Sweet blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I ave been trying for a while but I never seem to get there! Appreciate it

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>