问题描述
我要附加到正在运行的过程中使用DDD,我手工做的是:
I want to attach to a running process using 'ddd', what I manually do is:
# ps -ax | grep PROCESS_NAME
然后我得到一个列表和PID,那么I型:
Then I get a list and the pid, then I type:
# ddd PROCESS_NAME THE_PID
时有键入只有一个直接命令的方式?
Is there is a way to type just one command directly?
备注:当我键入 PS -ax | grep的PROCESS_NAME
中,grep将匹配的进程和的grep
命令行本身。
Remark: When I type ps -ax | grep PROCESS_NAME
, grep will match both the process and grep
command line itself.
推荐答案
有一个简单的方法来摆脱grep的过程:
There is an easy way to get rid of the grep process:
ps -ax | grep PROCESS_NAME | grep -v ' grep '
(只要你想找到不包含字符串的过程的grep
)。
所以,这样的事情应该在脚本中工作(同样,假设有只有一个副本运行):
So something like this should work in a script (again, assuming there's only one copy running):
pid=$(ps -ax | grep $1 | grep -v ' grep ' | awk '{print $1}')
ddd $1 ${pid}
如果你打电话给你的脚本 dddproc
,你可以调用它:
If you call your script dddproc
, you can call it with:
dddproc myprogramname
虽然我想补充一些完整性检查,如检测是否有零个或多个进程从 PS
返回,并确保用户提供的参数。
Although I'd add some sanity checks such as detecting if there's zero or more than one process returned from ps
and ensuring the user supplies an argument.
这篇关于shell命令找到一个进程ID和重视呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!