Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

4 Answers 4

up vote 6 down vote accepted

In your script:

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

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

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

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? :) –  foxbunny 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

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.