How do I find out the process id of the backgrounded process?

Posted on

How do I find out the process id of the backgrounded process? – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, command-line-interface, daemon, nohup, .

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.

Solution :

In your script:

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

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

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 }'

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.

Leave a Reply

Your email address will not be published.