3

I want to attach to a running process using 'ddd', what I manually do is:

# ps -ax | grep PROCESS_NAME

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?

Remark: When I type ps -ax | grep PROCESS_NAME, grep will match both the process and grep command line itself.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Elias Bachaalany
  • 1,180
  • 2
  • 13
  • 27
  • 2
    You can eliminate `grep -v grep` by using `grep [P]ROCESS_NAME` - putting square brackets around the first character of the process name makes the shell interpret that as one of a list of one character ("P" in this case) but `grep` sees the literal bracket-P-bracket in the output of `ps` so it doesn't match. – Dennis Williamson May 14 '10 at 11:12

6 Answers6

3

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'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}

If you call your script dddproc, you can call it with:

dddproc myprogramname

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Your answer is half the solution. I was expecting some grep command that would yield only the PID, so then I can do something like: ddd PROCESS_NAME $(THE_MAGIC_GREP_COMMAND) – Elias Bachaalany May 14 '10 at 10:27
  • @lallous, the `grep | grep -v | awk` will give you the PID of the processs. `grep -v grep` strips out the (grep process) line you're complaining about and `awk '{print $1}' gives you just the PID. – paxdiablo May 14 '10 at 10:31
1

As separate commands:

% PID=`ps -ax | grep ${PROCESS_NAME} | grep -v grep | cut -d ' ' -f 1-2`
% ddd ${PROCESS_NAME} ${PID}

In one line:

% PID=`ps -ax | grep ${PROCESS_NAME} | grep -v grep | cut -d ' ' -f 1-2` && ddd ${PROCESS_NAME} ${PID}
Paul R
  • 208,748
  • 37
  • 389
  • 560
0

Do this way -

ddd PROCESS_NAME \`ps -ax | grep PROCESS_NAME | grep -v grep | awk '{print $1}'\`
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Kiran
  • 1
0
ddd <process_name> `pgrep <process_name>`
Casual Coder
  • 1,492
  • 2
  • 14
  • 15
0

you can use pggrep to find the process

anish
  • 1,035
  • 4
  • 13
  • 27
0

You can use awk to both filter and get the column you want. The "exit" limits the 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 have to adjust the columns for your ps output. Also you can change the == to ~ for regex matching.

Randy Proctor
  • 7,396
  • 3
  • 25
  • 26