shell脚本来获得在Linux上的进程ID [英] Shell script to get the process ID on Linux

查看:182
本文介绍了shell脚本来获得在Linux上的进程ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个shell脚本( .SH 文件),以获得一个给定的进程ID。我想在这里做的是,一旦我得到的进程ID,我要杀死该进程。我的Ubuntu上运行(Linux)的。

I want to write a shell script (.sh file) to get a given process id. What I'm trying to do here is once I get the process ID, I want to kill that process. I'm running on Ubuntu (Linux).

我能够像

ps -aux|grep ruby
kill -9 <pid>

但我不知道如何通过shell脚本做到这一点。

but I'm not sure how to do it through a shell script.

推荐答案

使用的grep PS 的结果是一个坏主意在脚本中,因为时间一定比例也将匹配你刚才援引的grep的过程。该命令 指派, 避免了这个问题,所以如果你需要知道进程的ID,这是一个更好的选择。 (注意,当然,可以有许多方法匹配。)

Using grep on the results of ps is a bad idea in a script, since some proportion of the time it will also match the grep process you've just invoked. The command pgrep avoids this problem, so if you need to know the process ID, that's a better option. (Note that, of course, there may be many processes matched.)

不过,在你的榜样,你可以只使用了类似的命令 pkill的来杀死所有匹配的进程:

However, in your example, you could just use the similar command pkill to kill all matching processes:

pkill ruby

顺便说一句,你应该知道,使用 -9 是矫枉过正(嗬嗬)在几乎所有情况下 - 有中的无用的文字有关的一些有用的建议使用杀死-9 套用信函:

Incidentally, you should be aware that using -9 is overkill (ho ho) in almost every case - there's some useful advice about that in the text of the "Useless Use of kill -9 form letter ":

不不不。不要使用杀死-9

它不给进程一个机会,干净:

It doesn't give the process a chance to cleanly:


      
  1. 关闭套接字连接

  2.   
  3. 清理临时文件

  4.   
  5. 通知其子女,这是要离开

  6.   
  7. 重置其终端特点

  8.   

等等等等等等。

一般情况下,送15,并等待一两秒钟,并且如果不
  工作,送2,如果不工作,发送1.如果还是不行,
  卸下BINARY,因为该程序是表现不好!

Generally, send 15, and wait a second or two, and if that doesn't work, send 2, and if that doesn't work, send 1. If that doesn't, REMOVE THE BINARY because the program is badly behaved!

不要使用杀死-9 。不要带出的联合收割机刚刚整理
  了花盆。

Don't use kill -9. Don't bring out the combine harvester just to tidy up the flower pot.

这篇关于shell脚本来获得在Linux上的进程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆