5

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.

4 Answers4

7

In your script:

nohup command > logfile.txt &
echo $! > /var/run/command.pid
leonbloy
  • 2,028
  • 17
  • 23
3

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

Phil Hollenback
  • 14,647
  • 4
  • 34
  • 51
2

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 }'
Massimo
  • 68,714
  • 56
  • 196
  • 319
0

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.

Publiccert
  • 1,110
  • 1
  • 8
  • 22
  • 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? :) –  Jan 10 '11 at 22:33