A shell to find the process ID and attach to it?
I want to connect to a running process using 'ddd', which I do manually:
# ps -ax | grep PROCESS_NAME
Then I get the list and pid, then I print:
# ddd PROCESS_NAME THE_PID
Is there a way to print just one command directly?
Note. When I type ps -ax | grep PROCESS_NAME
grep will match the process command line itself and grep
.
a source to share
There is an easy way to get rid of the grep process:
ps -ax | grep PROCESS_NAME | grep -v ' grep '
(as long as the process you are trying to find does not include a string " grep "
).
So, something like this should work in a script (again, assuming only one copy works):
pid=$(ps -ax | grep $1 | grep -v ' grep ' | awk '{print $1}')
ddd $1 ${pid}
If you are calling your script dddproc
, you can call it with:
dddproc myprogramname
Although I would add some sanity checks, such as detecting if ps
zero or more processes were returned from , and for the user to provide an argument.
a source to share
You can use awk to filter and get the column you want. "Exit" limits ps results to the first hit.
function ddd_grep() {
ddd $(ps -ax | awk -v p="$1" '$4 == p { print $1; exit 0; }');
}
ddd_grep PROCESS_NAME
You may need to adjust the columns for ps output. Also you can change ==
to ~
to match regular expressions.
a source to share