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

.

+2


a source to share


6 answers


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.

+2


a source


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}

      

+1


a source


Do this way -

ddd PROCESS_NAME \`ps -ax | grep PROCESS_NAME | grep -v grep | awk '{print $1}'\`

      

0


a source


ddd <process_name> `pgrep <process_name>`

      

0


a source


you can use pggrep to find the process

0


a source


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.

0


a source







All Articles