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.

I'm redirecting results for something into a file and I also need to find the process name and ID number and redirect it to the file as well. I'm guessing the process name is the command in which I'm directing the results to. As in if the command is pgrep sshd and I redirect the results to the file, pgrep sshd would be the process name. I'm just curious though how exactly I find the process ID number and what would be the simplest way to redirect it to a file if there is a command to find it.

Here is the original objective I was given, I'm sorry if I seem really new at this, I am a student.

Man pages usually have a helpful section near the end called "SEE ALSO" that you can use to find a list of commands and topics related to the command that the current man page is about. Use this feature to locate a command related to "ps" that will search for processes based on some criteria. Use this command to locate all processes called sshd, directing the output to a file named processes.txt in ~/sysadmin1, making sure that the command also prints out the process name next to the process ID number. You will have to specify a flag with the command that you find in the man page for the command in order to do this. When you have finished add the full command used to the top of the processes.txt file.

share|improve this question
    
Do what your instructions tell you to do. Read the man page to find the appropriate switch, the answer is right there! –  Joseph R. Nov 8 '13 at 23:57
    
If I understand it correctly you want to grep the process id but also the associated process name. E.g if I run sshd and it's pid is 22143 and I wanted the output into a file as 22143 /usr/sbin/sshd I would run something like ps aux|grep [s]shd | awk '{print $2, $(NF-1)}' > processes.txt –  val0x00ff Nov 9 '13 at 0:09
    
@val0x00ff The instructions say to look in the ps man page for a similar command that "will search processes based on some criteria". –  Joseph R. Nov 9 '13 at 0:15
    
@JosephR. that's right. man ps has detailed information. I'm just providing some ideas. What I could get from the manual. This would be another example ps xgc | awk '/[s]shd/{$2=$3=$4=""; print }' > processes.txt –  val0x00ff Nov 9 '13 at 0:23
2  
@val0x00ff What I'm saying is, the instructions say not to use the ps command. –  Joseph R. Nov 9 '13 at 0:32

1 Answer 1

up vote 2 down vote accepted

I opened a shell and typed "man ps" and then foudn the SEE ALSO section. Here's what it is on my Mint 14 system:

SEE ALSO
       pgrep(1), pstree(1), top(1), proc(5).

Your instructions say to pick one of those and use it to list all the processes named "sshd." In this case, pgrep is your friend. Read the man page for pgrep (man pgrep) to learn how to make pgrep spit out the process name along with the PID for the sshd processes.

On my system, I see that the "-l" option will do it:

$ pgrep -l sshd
10247 sshd
share|improve this answer
1  
How does this help the OP learn on their own? –  Joseph R. Nov 9 '13 at 7:50

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.