我在服务器上运行nohup进程。当我试图杀死它,我的油灰控制台关闭。

这是我如何试图找到进程ID:

ps -ef |grep nohup 

这是杀人的命令

 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 keyword/command itself does not appear in the ps output for the command in question.

如果你使用一个脚本,你可以在脚本中做这样的事情:

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.

如果该命令定期发送输出,您可以偶尔使用tail my.log检查输出,或者如果您希望“实时”跟踪它,则可以使用tail -f my.log。最后,如果你需要终止进程,你可以通过:

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

其他回答

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

如果该命令定期发送输出,您可以偶尔使用tail my.log检查输出,或者如果您希望“实时”跟踪它,则可以使用tail -f my.log。最后,如果你需要终止进程,你可以通过:

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

Jobs -l应该为您提供nohup进程列表的pid。 轻轻地杀死他们。 ;)

如果您不知道PID,那么首先使用TOP命令找到它

top -U用户id

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

您将使用top获取PID,然后执行终止操作。

$ kill -9 <PID>

假设我在后台用下面的命令运行ruby脚本

nohup ruby script.rb &

然后我可以通过指定命令名来获取上述后台进程的pid。在我的例子中,命令是ruby。

ps -ef | grep ruby

输出

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

现在您可以使用kill命令轻松地终止进程

kill 25938

当你在nohup中创建一个作业时,它会告诉你进程ID !

nohup sh test.sh &

输出将显示进程ID,如

25013

你可以杀死它:

kill 25013