Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am running various programs on Unix in the background. Each program is outputting into a different file, e.g.:

python eventsim.py > logfile &
python eventsim.py > log1 &

Now say I want to kill the process that outputs to the file log1, but I no longer remember what its pid is. Is there a way I can retrieve that info?

share|improve this question

migrated from stackoverflow.com Feb 14 '13 at 23:33

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

Check the jobs command will print the currently running jobs.

jobs

After you get the process id, you can use fg or kill.

You can also use a combination of ps and grep, or pgrep (if available):

pgrep eventsim.py

Note: This isn't really programming related and might be better answered at http://superuser.com/.

share|improve this answer
    
so using jobs, nothing comes up. I am using bash and I get nothing, however on a sever that I am connected to uses tcsh and I get what I want using jobs...why? Isn't bash Unix? – guy_without_a_name Feb 14 '13 at 21:30
2  
bash (and tcsh) is a shell that is commonly on unix systems, but it is not unix. jobs should work on both if you have a background process. – gpojd Feb 14 '13 at 21:32
    
Thanks for clarification – guy_without_a_name Feb 14 '13 at 21:33

You should be able to see the full command line using ps(1):

ps auxwww | grep '[l]og1'

The PID will be in the second column.

share|improve this answer
    
For some reason, it gives different pid – guy_without_a_name Feb 14 '13 at 21:39
    
A different PID to what? – Johnsyweb Feb 14 '13 at 21:50
    
say the pid for python eventsim.py is 722, when i use that command, i get a pid of 1532 – guy_without_a_name Feb 14 '13 at 21:52
2  
I thought you didn't know the PID? Are you sure 722 isn't the PID of another instance of python eventsim.py? grep '[l]og1' was specifically to find instances writing to that particular file. – Johnsyweb Feb 14 '13 at 21:58

try this command:

ps aux | grep 'eventsim.py' | awk '{print $2,$11,$12}'

the output is:

1595 python eventsim.py

1595 is the pid number

and now:

kill <pid-number>

and the proccess will terminate

share|improve this answer
    
I would also like to see the output file – guy_without_a_name Feb 14 '13 at 21:38
    
Why use grep and not ps aux | awk '/eventsim.py/ {print $2,$11,$12}'? – Johnsyweb Feb 14 '13 at 21:50
    
Johnsyweb because i'm learn about awk script ;) – user2047676 Feb 14 '13 at 22:06

I found a way around: kill -STOP [pid] provides a confirmation that shows what I am looking for. Thanks for the help!

share|improve this answer

Your Answer

 
discard

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