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.

link|improve this question
feedback

4 Answers

up vote 3 down vote accepted

In your script:

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

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

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

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.

link|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
feedback

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 }'
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.