[linux] How to get the process ID to kill a nohup process?

I'm running a nohup process on the server. When I try to kill it my putty console closes instead.

this is how I try to find the process ID:

ps -ef |grep nohup 

this is the command to kill

 kill -9 1787 787

This question is related to linux bash grep nohup

The answer is


I often do this way. Try this way :

ps aux | grep script_Name

Here, script_Name could be any script/file run by nohup. This command gets you a process ID. Then use this command below to kill the script running on nohup.

kill -9 1787 787

Here, 1787 and 787 are Process ID as mentioned in the question as an example. This should do what was intended in the question.


jobs -l should give you the pid for the list of nohup processes. kill (-9) them gently. ;)


I am using red hat linux on a VPS server (and via SSH - putty), for me the following worked:

First, you list all the running processes:

ps -ef

Then in the first column you find your user name; I found it the following three times:

  • One was the SSH connection
  • The second was an FTP connection
  • The last one was the nohup process

Then in the second column you can find the PID of the nohup process and you only type:

kill PID 

(replacing the PID with the nohup process's PID of course)

And that is it!

I hope this answer will be useful for someone I'm also very new to bash and SSH, but found 95% of the knowledge I need here :)


This works for mi fine on mac

kill -9 `ps -ef | awk '/nohup/{ print \$2 }'`

if you are on a remote server, check memory usage with top , and find your process and its ID. After that, just execute kill [your process ID] .


when you create a job in nohup it will tell you the process ID !

nohup sh test.sh &

the output will show you the process ID like

25013

you can kill it then :

kill 25013

Suppose you are executing a java program with nohup you can get java process id by

`ps aux | grep java`

output

xxxxx     9643  0.0  0.0  14232   968 pts/2   

then you can kill the process by typing

sudo kill 9643

or lets say that you need to kill all the java processes then just use

sudo killall java

this command kills all the java processes. you can use this with process. just give the process name at the end of the command

sudo killall {processName}

About losing your putty: often the ps ... | awk/grep/perl/... process gets matched, too! So the old school trick is like this

ps -ef | grep -i [n]ohup 

That way the regex search doesn't match the regex search process!


suppose i am running ruby script in the background with below command

nohup ruby script.rb &

then i can get the pid of above background process by specifying command name. In my case command is ruby.

ps -ef | grep ruby

output

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

Now you can easily kill the process by using kill command

kill 25938

Today I met the same problem. And since it was a long time ago, I totally forgot which command I used and when. I tried three methods:

  1. Using the STIME shown in ps -ef command. This shows the time you start your process, and it's very likely that you nohup you command just before you close ssh(depends on you) . Unfortunately I don't think the latest command is the command I run using nohup, so this doesn't work for me.
  2. Second is the PPID, also shown in ps -ef command. It means Parent Process ID, the ID of process that creates the process. The ppid is 1 in ubuntu for process that using nohup to run. Then you can use ps --ppid "1" to get the list, and check TIME(the total CPU time your process use) or CMD to find the process's PID.
  3. Use lsof -i:port if the process occupy some ports, and you will get the command. Then just like the answer above, use ps -ef | grep command and you will get the PID.

Once you find the PID of the process, then can use kill pid to terminal the process.


I started django server with the following command.

nohup manage.py runserver <localhost:port>

This works on CentOS:

:~ ns$netstat -ntlp
:~ ns$kill -9 PID 

If your application always uses the same port, you can kill all the processes in that port like this.

kill -9 $(lsof -t -i:8080)


This works in Ubuntu

Type this to find out the PID

ps aux | grep java

All the running process regarding to java will be shown

In my case is

johnjoe      3315  9.1  4.0 1465240 335728 ?      Sl   09:42   3:19 java -jar batch.jar

Now kill it kill -9 3315

The zombie process finally stopped.


If you are unaware of the PID, then first find it using TOP command

top -U userid

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND

You will get the PID using top, then perform the kill operation.

$ kill -9 <PID>

You could try

kill -9 `pgrep [command name]`

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to grep

grep's at sign caught as whitespace cat, grep and cut - translated to python How to suppress binary file matching results in grep Linux find and grep command together Filtering JSON array using jQuery grep() Linux Script to check if process is running and act on the result grep without showing path/file:line How do you grep a file and get the next 5 lines How to grep, excluding some patterns? Fast way of finding lines in one file that are not in another?

Examples related to nohup

How to get the process ID to kill a nohup process? How to get a list of programs running with nohup What's the difference between nohup and ampersand Can I change the name of `nohup.out`? How do I put an already-running process under nohup?