如何获取进程ID杀死一个nohup的过程? [英] How to get the process ID to kill a nohup process?

查看:1998
本文介绍了如何获取进程ID杀死一个nohup的过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行在服务器上的nohup进程。当我尝试要杀死它我腻子控制台关闭吧。

这就是我试图找到进程ID:

  PS -ef | grep的的nohup

这是命令杀死

 杀-9 1787年787


解决方案

在使用的nohup ,你把任务在​​后台,后台操作者(&安培; )会给你在命令提示符下的PID。如果你的计划是手动管理的过程中,你可以保存PID,并用它后来杀死如果需要的过程中,通过杀PID 杀-9 PID (如果你需要强制中止)。另外,您也可以通过 PS -ef找到PID以后| grep的命令名并从那里找到的PID。需要注意的是的nohup 未出现在 PS 输出。

如果您使用的脚本,你可以这样做:

 的nohup my_command> my.log 2  - ;&放大器; 1和;
回声$! > save_pid.txt

这将运行 my_command 保存所有的输出到 my.log (在脚本中, $!重新presents最后执行的过程)的PID。在 2 是标准差(标准错误)和 2 - 文件描述符;&放大器; 1 告诉shell路由标准错误输出到标准输出(文件描述符 1 )。它要求&放大器; 1 ,以便shell知道这是在这种情况下,而不是 1 命名只是一个文件的文件描述符。在 2 - ;&放大器; 1 是需要捕捉,通常被写入标准错误到任何错误消息,我们的 my.log 文件(这是从标准输出来)。请参见 I / O的更多详细信息,重定向在处理I / O重定向与外壳。

如果命令定期发送输出,你可以偶尔尾my.log 检查输出,或者,如果你想跟着它活,您可使用尾-f my.log 。最后,如​​果你需要杀死的过程中,您可以通过做到这一点:

 杀-9`猫save_pid.txt`

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

解决方案

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 does not appear in the ps output.

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

这篇关于如何获取进程ID杀死一个nohup的过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆