Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

There can be more than one instance of ssh running:

$ ps aux | grep ssh
cpn       6098  0.0  0.0  58196  2032 ?        S    10:08   0:01 ssh cz -nNCTR 5433:localhost4:5432
root      6313  0.0  0.0  64072  1168 ?        Ss   12:22   0:00 /usr/sbin/sshd
root      6504  0.0  0.0  97816  3856 ?        Ss   15:48   0:00 sshd: cpn [priv] 
cpn       6508  0.0  0.0  97816  1780 ?        S    15:49   0:00 sshd: cpn@pts/0  
cpn       6552  0.0  0.0  57680   936 ?        Ss   16:16   0:00 ssh -fNL 5433:localhost4:5433 cz
cpn       6554  0.0  0.0 103236   860 pts/0    S+   16:16   0:00 grep ssh

pidof returns all running ssh pids:

$ pidof ssh
6552 6098

I need to find the pid of the one with the reverse connection (-nNCTR).

share|improve this question
2  
have you tried pgrep? –  cha0site Feb 1 '13 at 16:31
add comment

migrated from stackoverflow.com Feb 1 '13 at 20:35

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 3 down vote accepted

Give pgrep a go:

pgrep -f 'ssh .* -nNCTR'
share|improve this answer
add comment

I'd do this

ps axu |grep 'nNCTR'

ps axu is great to grep from but there is one small problem! The grep process itself!

ps axu |grep 'nNCTR' |grep -v grep

will exclude this too

share|improve this answer
    
Use ps axu | grep '[n]NCTR' to avoid the second grep. Better yet, use pgrep. –  Shawn Chin Feb 1 '13 at 16:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.