Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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've got a command (program) that I'd like to run with nohup and background it. Like

nohup command > logfile.txt &

How do I find out the process ID? I would like to be able to write the process id in a file, read it later and kill the process programatically.

share|improve this question
up vote 6 down vote accepted

In your script:

nohup command > logfile.txt &
echo $! > /var/run/
share|improve this answer
Thanks for this! That's exactly what I wanted. – hayavuk Jan 10 '11 at 22:33

You can use $!. Referenced in the bash documentation.

share|improve this answer
Thanks for the docs link. – hayavuk Jan 10 '11 at 22:33

You can use ps and grep to find the process in the process list and then awk to parse the output and find the actual PID:

ps -ef | grep -v grep | grep YOUR_PROCESS_NAME | awk '{ print $2 }'
share|improve this answer

You could try:

ps aux | grep -v grep | grep program name

That'll check the running processes, grep for the program name, but exclude the grep itself.

share|improve this answer
This solution would not work because I run multiple instances of the same program (2 to be exact), and I need to differentiate between them. EDIT: I think it wouldn't work... would it? :) – hayavuk Jan 10 '11 at 22:33

Your Answer


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.