Coder Perfect

How do I retrieve the process ID of a nohup process in order to kill it?

Problem

On the server, I’m running a nohup process. My putty console closes instead of killing it when I try to kill it.

This is how I go about looking for the process ID:

ps -ef |grep nohup 

This is the kill command.

 kill -9 1787 787

Asked by user2535056

Solution #1

When you use nohup and run the task in the background, the background operator (&) will prompt you for the PID. If you intend to manage the process manually, you can save the PID and use it to stop the process if necessary, using kill PID or kill -9 PID (if you need to force kill). Alternatively, you may find the PID afterwards by running ps -ef | grep “command name” and then looking for it. It’s worth noting that the nohup keyword/command does not appear in the ps output for the command at hand.

If you’re using a script, you could write something like this:

nohup my_command > my.log 2>&1 &
echo $! > save_pid.txt

This will execute my command and save all output to my.log (in a script, $! denotes the PID of the most recently executed process). The 2 is the standard error (stderr) file descriptor, and 2>&1 directs the shell to send standard error output to standard output (file descriptor 1). It requires &1 so that the shell recognizes it as a file descriptor rather than merely a file named 1 in that context. The 2>&1 is required to capture any error messages delivered to standard error into our my.log file (which is coming from standard output). For additional information on using the shell to handle I/O redirection, see I/O Redirection.

If the command produces output on a regular basis, you can verify it with tail my.log or use tail -f my.log to follow it “live.” Finally, if you need to terminate the process, use the following commands:

kill -9 `cat save_pid.txt`
rm save_pid.txt

Answered by lurker

Solution #2

I’m using Red Hat Linux on a VPS server (and using putty for SSH), and the following worked for me:

To begin, make a list of all the processes that are now active:

ps -ef

Then look for your user name in the first column; I found mine three times:

Then, in the second column, you’ll see the nohup process’s PID, and all you have to do is type:

kill PID 

(Of course, the PID should be replaced with the PID of the nohup process.)

And that is it!

I hope this response is useful to someone. I’m new to bash and SSH as well, but I’ve discovered 95 percent of the information I need here:)

Answered by Phonebox

Solution #3

Assume I’m executing a ruby script in the background using the command below.

nohup ruby script.rb &

Then, by supplying the command name, I can get the pid of the aforesaid background process. Ruby is the command in my situation.

ps -ef | grep ruby

output

ubuntu   25938 25742  0 05:16 pts/0    00:00:00 ruby test.rb

You may now quickly terminate the process by using the kill command.

kill 25938

Answered by Sanjay Salunkhe

Solution #4

The pid for the list of nohup processes should be returned by jobs -l. 😉 Kill them gently (-9)

Answered by Puneet S. Chauhan

Solution #5

You could try

kill -9 `pgrep [command name]`

Answered by Balliver

Post is based on https://stackoverflow.com/questions/17385794/how-to-get-the-process-id-to-kill-a-nohup-process