shell命令查找进程ID并附加到它? [英] shell command to find a process id and attach to it?
问题描述
我想使用ddd"附加到正在运行的进程,我手动执行的是:
I want to attach to a running process using 'ddd', what I manually do is:
# ps -ax | grep PROCESS_NAME
然后我得到一个列表和 pid,然后我输入:
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 "
).
(as long as the process you're trying to find doesn't include the string " 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并附加到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!