Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

If I run sleep 30 in one PuTTY window, how can I find the process ID of the sleep command from another PuTTY window?

share|improve this question
3  
Sounds like homework. –  Hauke Laging May 25 at 23:34
1  
@HaukeLaging - what's wrong with homework? –  mikeserv May 26 at 2:06
    
My brother never did his homework by himself. Now he's 28. He's still happy and unemployed:-) –  Slyx May 26 at 4:02
1  
@mikeserv I don't see a need to discuss why homework should not be made by others. Questions without any research effort, without any precise question but with a "do all the work for me" attitude are bad questions even under other circumstances. In a homework case Im find that embarrassing. –  Hauke Laging May 26 at 5:02
1  
@HaukeLaging - don't make the homework. Help teach. –  mikeserv May 26 at 7:21

2 Answers 2

The pgrep Command

If your distribution has the pgrep command installed (e.g. from the procps package), then you can use the command to find process IDs by pattern. For example:

# Find the sleep commands belonging to the current user.
pgrep -u $LOGNAME sleep

This command will find all matches belonging to the current user, whether the process was started in the current terminal or another, so it will work for your PuTTY use case.

Testing the pgrep Command

You can test this out on a single TTY or PTY quite simply, too. Consider:

$ sleep 30 & pgrep -u $LOGNAME sleep
[1] 24021
24021

In this example, sleep is put into the background and Bash reports the Job ID and PID of the backgrounded job. Then pgrep finds the same PID in the process list, and sends the PID to standard output. Obviously, the numbers should match.

Multiple Matches

Note that if the current user has more than one sleep command running, then pgrep will correctly report multiple PIDs. For example, the following returns PIDs for background jobs %1 and %2:

$ sleep 30 & sleep 30 & pgrep -u $LOGNAME sleep
[1] 24063
[2] 24064
24063
24064
share|improve this answer
    
thank u very much –  jay May 26 at 20:30
ps -u "your_login" | grep "sleep" | awk '{print $1}'
share|improve this answer
    
thank u very much –  jay May 26 at 20:30

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.