Linux 如何获取进程 ID 以杀死 nohup 进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17385794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the process ID to kill a nohup process?
提问by user2535056
I'm running a nohup process on the server. When I try to kill it my putty console closes instead.
我正在服务器上运行 nohup 进程。当我尝试杀死它时,我的腻子控制台会关闭。
this is how I try to find the process ID:
这就是我尝试查找进程 ID 的方式:
ps -ef |grep nohup
this is the command to kill
这是杀死的命令
kill -9 1787 787
采纳答案by lurker
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.
使用时nohup
,您将任务置于后台,后台操作符 ( &
) 将在命令提示符下为您提供 PID。如果您的计划是手动管理进程,您可以保存该 PID,并在需要时使用它来终止进程,通过kill PID
or kill -9 PID
(如果您需要强制终止)。或者,您可以稍后ps -ef | grep "command name"
找到 PID,然后从那里找到 PID。请注意,nohup
关键字/命令本身不会出现在ps
相关命令的输出中。
If you used a script, you could do something like:
如果您使用脚本,您可以执行以下操作:
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 Redirectionfor more details on handling I/O redirection with the shell.
这将运行my_command
将所有输出保存到my.log
(在脚本中,$!
代表最后执行的进程的 PID)。的2
是标准误差(文件描述符stderr
)和2>&1
告诉shell路由标准错误输出到标准输出(文件描述符1
)。它要求&1
shell 知道它是该上下文中的文件描述符,而不仅仅是名为1
. 在2>&1
需要捕捉任何错误消息通常被写入标准错误到我们的my.log
文件(从标准输出来)。有关使用 shell 处理 I/O 重定向的更多详细信息,请参阅I/O 重定向。
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:
如果命令定期发送输出,您可以偶尔使用 来检查输出tail my.log
,或者如果您想“实时”关注它,您可以使用tail -f my.log
。最后,如果您需要终止进程,可以通过以下方式完成:
kill -9 `cat save_pid.txt`
rm save_pid.txt
回答by Phonebox
I am using red hat linux on a VPS server (and via SSH - putty), for me the following worked:
我在 VPS 服务器上使用 red hat linux(并通过 SSH - putty),对我来说以下工作:
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
- 一个是SSH连接
- 第二个是FTP连接
- 最后一个是nohup过程
Then in the second column you can find the PID of the nohup process and you only type:
然后在第二列中,您可以找到 nohup 进程的 PID,您只需键入:
kill PID
(replacing the PID with the nohup process's PID of course)
(当然用 nohup 进程的 PID 替换 PID)
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 :)
我希望这个答案对我对 bash 和 SSH 也很陌生的人有用,但在这里找到了我需要的 95% 的知识:)
回答by Balliver
You could try
你可以试试
kill -9 `pgrep [command name]`
回答by Sanjay Salunkhe
suppose i am running ruby script in the background with below command
假设我正在使用以下命令在后台运行 ruby 脚本
nohup ruby script.rb &
then i can get the pid of above background process by specifying command name. In my case command is ruby.
然后我可以通过指定命令名称来获取上述后台进程的pid。在我的情况下,命令是 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 命令轻松终止进程
kill 25938
回答by Puneet S. Chauhan
jobs -lshould give you the pid for the list of nohup processes. kill (-9) them gently. ;)
jobs -l应该为您提供 nohup 进程列表的 pid。轻轻地杀死 (-9) 它们。;)
回答by John Joe
This works in Ubuntu
这适用于 Ubuntu
Type this to find out the PID
输入这个以找出 PID
ps aux | grep java
All the running process regarding to java will be shown
将显示所有有关 java 的运行过程
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
现在杀了它 kill -9 3315
The zombie process finally stopped.
僵尸进程终于停止了。
回答by Naseer-shaik
I started django server with the following command.
我使用以下命令启动了 Django 服务器。
nohup manage.py runserver <localhost:port>
This works on CentOS:
这适用于 CentOS:
:~ ns$netstat -ntlp
:~ ns$kill -9 PID
回答by NuOne
Suppose you are executing a java program with nohup you can get java process id by
假设您正在使用 nohup 执行一个 java 程序,您可以通过以下方式获取 java 进程 ID
`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
或者说你需要杀死所有的java进程然后使用
sudo killall java
this command kills all the java processors. you can use this with process. just give the process name at the end of the command
这个命令会杀死所有的 java 处理器。您可以将其与流程一起使用。只需在命令末尾给出进程名称
sudo killall {processName}
回答by broid
when you create a job in nohup it will tell you the process ID !
当您在 nohup 中创建作业时,它会告诉您进程 ID!
nohup sh test.sh &
the output will show you the process ID like
输出将向您显示进程 ID,例如
25013
you can kill it then :
然后你可以杀死它:
kill 25013
回答by Youth Wu
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:
今天我遇到了同样的问题。而且因为很久以前,我完全忘记了我使用的命令和时间。我尝试了三种方法:
- 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. - 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 useps --ppid "1"
to get the list, and check TIME(the total CPU time your process use) or CMD to find the process's PID. - Use
lsof -i:port
if the process occupy some ports, and you will get the command. Then just like the answer above, useps -ef | grep command
and you will get the PID.
- 使用
ps -ef
命令中显示的 STIME 。这显示了您启动进程的时间,并且很可能您在关闭 ssh(取决于您)之前 nohup 您命令。不幸的是,我认为最新的命令不是我使用 nohup 运行的命令,所以这对我不起作用。 - 第二个是 PPID,也显示在
ps -ef
命令中。它表示父进程 ID,即创建进程的进程 ID。对于使用 nohup 运行的进程,ubuntu 中的 ppid 为 1。然后您可以使用ps --ppid "1"
获取列表,并检查 TIME(您的进程使用的总 CPU 时间)或 CMD 以查找进程的 PID。 - 使用
lsof -i:port
如果该进程占用一些端口,你会得到的命令。然后就像上面的答案一样,使用ps -ef | grep command
,您将获得 PID。
Once you find the PID of the process, then can use kill pid
to terminal the process.
一旦找到进程的PID,就可以kill pid
用来终止进程。