How to Find the Process ID of a Backgrounded Process in Linux

command-line-interfacedaemonlinuxnohup

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.

Best Answer

In your script:

nohup command > logfile.txt &
echo $! > /var/run/command.pid