如何防止在 Linux 中关闭 SSH 客户端后后台进程停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/285015/
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 prevent a background process from being stopped after closing SSH client in Linux
提问by GetFree
I'm working on a Linux machine through SSH (Putty). I need to leave a process running during the night, so I thought I could do that by starting the process in background (with an ampersand at the end of the command) and redirecting stdout to a file.
我正在通过 SSH (Putty) 在 Linux 机器上工作。我需要让一个进程在夜间运行,所以我想我可以通过在后台启动进程(命令末尾有一个&符号)并将stdout重定向到一个文件来做到这一点。
To my surprise, that doesn't work. As soon as I close the Putty window, the process is stopped.
令我惊讶的是,这行不通。一旦我关闭 Putty 窗口,该过程就会停止。
How can I prevent that from happening??
我怎样才能防止这种情况发生??
回答by gpojd
I would recommend using GNU Screen. It allows you to disconnect from the server while all of your processes continue to run. I don't know how I lived without it before I knew it existed.
我建议使用GNU Screen。它允许您在所有进程继续运行时断开与服务器的连接。在我知道它存在之前,我不知道没有它我是如何生活的。
回答by Deon
Use screen. It is very simple to use and works like vnc for terminals. http://www.bangmoney.org/presentations/screen.html
使用屏幕。它使用起来非常简单,就像终端的 vnc 一样。 http://www.bangmoney.org/presentations/screen.html
回答by Robert Gamble
When the session is closed the process receives the SIGHUP signal which it is apparently not catching. You can use the nohup
command when launching the process or the bash built-in command disown -h
after starting the process to prevent this from happening:
当会话关闭时,进程收到 SIGHUP 信号,它显然没有捕捉到。您可以nohup
在启动进程时使用该命令或在启动进程后使用 bash 内置命令disown -h
来防止这种情况发生:
> help disown
disown: disown [-h] [-ar] [jobspec ...]
By default, removes each JOBSPEC argument from the table of active jobs.
If the -h option is given, the job is not removed from the table, but is
marked so that SIGHUP is not sent to the job if the shell receives a
SIGHUP. The -a option, when JOBSPEC is not supplied, means to remove all
jobs from the job table; the -r option means to remove only running jobs.
回答by Brian Knoblauch
nohup blah &
Substitute your process name for blah!
将您的进程名称替换为 blah!
回答by jcodeninja
Nohup allows a client process to not be killed if a the parent process is killed, for argument when you logout. Even better still use:
Nohup 允许在父进程被终止时不终止客户端进程,作为注销时的参数。更好的是仍然使用:
nohup /bin/sh -c "echo $$ > $pidfile; exec $FOO_BIN $FOO_CONFIG " > /dev/null
Nohup makes the process you start immune to termination which your SSH session and its child processes are kill upon you logging out. The command i gave provides you with a way you can store the pid of the application in a pid file so that you can correcly kill it later and allows the process to run after you have logged out.
Nohup 使您启动的进程不受终止的影响,您的 SSH 会话及其子进程在您注销时会被终止。我给的命令为您提供了一种方法,您可以将应用程序的 pid 存储在 pid 文件中,以便您以后可以正确地终止它,并在您注销后允许该进程运行。
回答by Jonathan Leffler
As others have noted, to run a process in the background so that you can disconnect from your SSH session, you need to have the background process properly disassociate itself from its controlling terminal - which is the pseudo-tty that the SSH session uses.
正如其他人所指出的,要在后台运行一个进程以便您可以与 SSH 会话断开连接,您需要让后台进程正确地将其自身与其控制终端分离——这是 SSH 会话使用的伪 tty。
You can find information about daemonizing processes in books such as Stevens' "Advanced Network Program, Vol 1, 3rd Edn" or Rochkind's "Advanced Unix Programming".
您可以在 Stevens 的“Advanced Network Program, Vol 1, 3rd Edn”或 Rochkind 的“Advanced Unix Programming”等书籍中找到有关守护进程的信息。
I recently (in the last couple of years) had to deal with a recalcitrant program that did not daemonize itself properly. I ended up dealing with that by creating a generic daemonizing program - similar to nohup but with more controls available.
我最近(在过去几年中)不得不处理一个顽固的程序,它没有正确地自我守护。我最终通过创建一个通用的守护程序来解决这个问题 - 类似于 nohup 但有更多可用的控件。
Usage: daemonize [-abchptxV][-d dir][-e err][-i in][-o out][-s sigs][-k fds][-m umask] -- command [args...]
-V print version and exit
-a output files in append mode (O_APPEND)
-b both output and error go to output file
-c create output files (O_CREAT)
-d dir change to given directory
-e file error file (standard error - /dev/null)
-h print help and exit
-i file input file (standard input - /dev/null)
-k fd-list keep file descriptors listed open
-m umask set umask (octal)
-o file output file (standard output - /dev/null)
-s sig-list ignore signal numbers
-t truncate output files (O_TRUNC)
-p print daemon PID on original stdout
-x output files must be new (O_EXCL)
The double-dash is optional on systems not using the GNU getopt() function; it is necessary (or you have to specify POSIXLY_CORRECT in the environment) on Linux etc. Since double-dash works everywhere, it is best to use it.
双破折号在不使用 GNU getopt() 函数的系统上是可选的;在 Linux 等上是必要的(或者您必须在环境中指定 POSIXLY_CORRECT)。由于双破折号在任何地方都可以使用,因此最好使用它。
You can still contact me (firstname dot lastname at gmail dot com) if you want the source for daemonize
.
如果您需要daemonize
.
However, the code is now (finally) available on GitHub in my SOQ(Stack
Overflow Questions) repository as file daemonize-1.10.tgz
in the
packagessub-directory.
但是,代码是现在(终于)在我的GitHub上可用的SOQ(堆栈溢出问题)存储库中的文件daemonize-1.10.tgz
在
包的子目录。
回答by Dana the Sane
If you use screen to run a process as root, beware of the possibility of privilege elevation attacks. If your own account gets compromised somehow, there will be a direct way to take over the entire server.
如果您使用 screen 以 root 身份运行进程,请注意特权提升攻击的可能性。如果您自己的帐户以某种方式被盗用,将有一种直接的方法来接管整个服务器。
If this process needs to be run regularly and you have sufficient access on the server, a better option would be to use cron the run the job. You could also use init.d (the super daemon) to start your process in the background, and it can terminate as soon as it's done.
如果此过程需要定期运行并且您在服务器上有足够的访问权限,则更好的选择是使用 cron 运行该作业。您还可以使用 init.d(超级守护进程)在后台启动您的进程,它可以在完成后立即终止。
回答by Will Hartung
Personally, I like the 'batch' command.
就个人而言,我喜欢“批处理”命令。
$ batch
> mycommand -x arg1 -y arg2 -z arg3
> ^D
This stuffs it in to the background, and then mails the results to you. It's a part of cron.
这会将其放入后台,然后将结果邮寄给您。它是 cron 的一部分。