316

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
2

17 Answers 17

525

When using nohup and you put the task in the background, the background operator (&) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via kill PID or kill -9 PID (if you need to force kill). Alternatively, you can find the PID later on by ps -ef | grep "command name" and locate the PID from there. Note that nohup keyword/command itself does not appear in the ps output for the command in question.

If you use a script, you could do something like this in the script:

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

This will run my_command saving all output into my.log (in a script, $! represents the PID of the last process executed). The 2 is the file descriptor for standard error (stderr) and 2>&1 tells the shell to route standard error output to the standard output (file descriptor 1). It requires &1 so that the shell knows it's a file descriptor in that context instead of just a file named 1. The 2>&1 is needed to capture any error messages that normally are written to standard error into our my.log file (which is coming from standard output). See I/O Redirection for more details on handling I/O redirection with the shell.

If the command sends output on a regular basis, you can check the output occasionally with tail my.log, or if you want to follow it "live" you can use tail -f my.log. Finally, if you need to kill the process, you can do it via:

kill -9 `cat save_pid.txt`
rm save_pid.txt
20
  • 26
    A slight clarification, it's not nohup per se, that prints the PID, it's the final & which backgrounds it, e.g. ls & would show the PID for running ls
    – karmakaze
    Commented Jan 7, 2016 at 2:29
  • 1
    What does the '2>&1' do ?
    – Viraj
    Commented Jul 9, 2016 at 4:39
  • 6
    @Viraj 2 is the "standard error" file descriptor. > is the shell redirection, and &1 is the "standard output" file descriptor (the & is needed here so the shell won't think I am referring to a file named 1). So 2 > &1 redirects any standard error output to the standard input. Since the prior > my.log means to redirect standard output to my.log, we need a way to ensure that error messages also go to my.log. 2 > &1 ensures that such errors go to standard output, which in turn goes to my.log. See I/O Redirection.
    – lurker
    Commented Jul 9, 2016 at 10:05
  • 7
    echo $! gives me the pid of nohup instead of the process spawned: paste.fedoraproject.org/428697/99695314 Commented Sep 16, 2016 at 3:36
  • 1
    @NehalJWani when you run nohup ... at the command line, it gives you the process Id of the command (in this case, python3) you are running. The nohup itself isn't running with its own process Id. In your example, if you were to kill -9 24246, your python3 process would be killed. To have a more complete example in your case, try ps ax | grep 24246 and see what's there.
    – lurker
    Commented Sep 16, 2016 at 10:09
151

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 :)

4
  • 3
    I think you meant PID instead of UID here?
    – wprins
    Commented Dec 3, 2015 at 17:18
  • I agree with @wprins. Killing the UID didn't work for me, but killing the PID did.
    – Ryan
    Commented Apr 17, 2017 at 18:34
  • 1
    In my case I ran a test shell script(long_running_script.sh) with nohup and & and dint know how to stop it. Finally, I did a ps -ef | grep long_running* and fouund the PID. Then did a kill PID Commented Jun 30, 2019 at 20:56
  • (Tangentially, grep long_running* is effectively identical to grep long_runnin, except if you run it in a directory where you have files which match the wildcard. If there is more than one match, this will do entirely the wrong thing. Generally, quote your regular expressions, and learn the differences between regular expressions and shell wildcards.)
    – tripleee
    Commented May 21, 2023 at 9:01
89

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
3
  • 2
    Sanjay, just so I'm sure I'm not missing anything, what part of your answer is new or different from the answers provided years ago? ps -ef and kill were both well covered above, so what is the new part? Commented Jun 23, 2015 at 5:41
  • 4
    ps -ef will give you the long list of output and searching from this long list will be difficult. so i think ps -ef | grep ruby is better command to search pid than just doing ps -ef Commented Jun 23, 2015 at 6:52
  • 1
    This answer helped me perfectly, Thanks!
    – iconique
    Commented Sep 22, 2020 at 2:01
35

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

2
  • 13
    Only if the job was started in the current shell. And don't kill -9 unless you know that regular signals do not work.
    – tripleee
    Commented Feb 4, 2016 at 6:46
  • 1
    This is useful, if you're on some docker image where ps isn't installed :-) Commented May 24, 2020 at 20:53
14

You could try

kill -9 `pgrep [command name]`
1
  • 4
    very nice! I used pkill [command name] you can use the -o flag for killing the oldest matching process, or -n to use the newest one instead.
    – zanona
    Commented Nov 6, 2017 at 12:29
14

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

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)

5

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.

4

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
1
  • This merely repeats information from the accepted answer from 2013. As pointed out there, the background operator &, not nohup, is what causes the PID to be printed by the shell.
    – tripleee
    Commented May 21, 2023 at 8:58
3

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 
2
  • 1
    This is the go to for solving the process not found confusion with nohup.
    – CodeSpent
    Commented Dec 3, 2018 at 15:50
  • You might need to add sudo to that command if you don't see the PID
    – flowfelis
    Commented Sep 25, 2023 at 13:53
2
ps -ef | grep <file_type>| grep <username>

For example, if you are running a Python file, use the following command:

ps -ef | grep python | grep user_name

It will list all the python processes that are initiated by you. Now you can kill those processes as:

kill -9 <p_id>
1

This works for mi fine on mac

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

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.

1

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>
0

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.

0

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!

1
  • 1
    The general idea is correct, but nohup will usually not show up in a ps listing because it's a shell builtin.
    – tripleee
    Commented May 21, 2023 at 8:56
0

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] .

Not the answer you're looking for? Browse other questions tagged or ask your own question.