shell command to find a process id and attach to it?

前端 未结 6 1712
感动是毒
感动是毒 2021-01-14 14:04

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

# ps -ax | grep PROCESS_NAME

Then I get a list and the pid, the

相关标签:
6条回答
  • 2021-01-14 14:06

    you can use pggrep to find the process

    0 讨论(0)
  • 2021-01-14 14:09

    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}')
    ffffd $1 ${pid}
    

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

    ffffdproc 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.

    0 讨论(0)
  • 2021-01-14 14:09
    ffffd <process_name> `pgrep <process_name>`
    
    0 讨论(0)
  • 2021-01-14 14:21

    As separate commands:

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

    In one line:

    % PID=`ps -ax | grep ${PROCESS_NAME} | grep -v grep | cut -d ' ' -f 1-2` && ffffd ${PROCESS_NAME} ${PID}
    
    0 讨论(0)
  • 2021-01-14 14:24

    You can use awk to both filter and get the column you want. The "exit" limits the ps results to the first hit.

    function ffffd_grep() {
      ffffd $(ps -ax | awk -v p="$1" '$4 == p { print $1; exit 0; }');
    }
    
    ffffd_grep PROCESS_NAME
    

    You may have to adjust the columns for your ps output. Also you can change the == to ~ for regex matching.

    0 讨论(0)
  • 2021-01-14 14:25

    Do this way -

    ffffd PROCESS_NAME \`ps -ax | grep PROCESS_NAME | grep -v grep | awk '{print $1}'\`
    
    0 讨论(0)
提交回复
热议问题