bash 如何将 ssh 作业发送到后台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8184717/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:04:51  来源:igfitidea点击:

how to send ssh job to background

bashsshio-redirection

提问by yasar

I logged in to a remote server via ssh and started a php script. Appereantly, it will take 17 hours to complete, is there a way to break the connection but the keep the script executing? I didn't make any output redirection, so I am seeing all the output.

我通过 ssh 登录到远程服务器并启动了一个 php 脚本。显然,这将需要 17 个小时才能完成,有没有办法断开连接但保持脚本执行?我没有做任何输出重定向,所以我看到了所有的输出。

采纳答案by Steve Rukuts

Can you stop the process right now? If so, launch screen, start the process and detach screen using ctrl-a then ctrl-d. Use screen -r to retrieve the session later.

你能立即停止这个过程吗?如果是这样,启动screen,启动进程并使用 ctrl-a 然后 ctrl-d 分离屏幕。稍后使用 screen -r 检索会话。

This should be available in most distros, failing that, a package will definitely be available for you.

这应该在大多数发行版中可用,否则,肯定会为您提供一个软件包。

回答by hovanessyan

ctrl + z 

will pause it. Than type

将暂停它。比型

bg

to send it to background. Write down the PID of the process for later usage ;)

将其发送到后台。记下进程的 PID 以备后用 ;)

EDIT: I forgot, you have to execute

编辑:我忘了,你必须执行

disown -$PID

where $PID is the pid of your process

其中 $PID 是您的进程的 pid

after that, and the process will not be killed after you close the terminal.

之后,关闭终端后该进程不会被终止。

回答by Znik

you described it's important to protect script continuation. Unfortunately I don't know, you make any interaction with script and script is made by you.

您描述了保护脚本连续性很重要。不幸的是,我不知道,您与脚本进行任何交互,而脚本是由您制作的。

  1. continuation protects 'screen' command. your connection will break, but screen protect pseudo terminal, you can reconnect to this later, see man.
  2. if you don't need operators interaction with script, you simply can put script to background at the start, and log complete output into log file. Simply use command:

    nohup /where/is/your.script.php >output.log 2&>1 &
    

    >output.logwill redirect output into log file, 2&>1will append error stream into output, effectively into log file. last &will put command into background. Notice, nohup command will detach process from terminal group.

    At now you can safely exit from ssh shell. Because your script is out of terminal group, then it won't be killed. It will be rejoined from your shell process, into system INIT process. It is unix like system behavior. Complete output you can monitor using command

    tail -f output.log   #allways breakable by ^C, it is only watching
    

    Using this method you do not need use ^Z , bg etc shell tricks for putting command to the background.

  1. 继续保护“屏幕”命令。你的连接会中断,但屏幕保护伪终端,你可以稍后重新连接,看看男人。
  2. 如果您不需要操作员与脚本的交互,您只需在开始时将脚本置于后台,并将完整的输出记录到日志文件中。只需使用命令:

    nohup /where/is/your.script.php >output.log 2&>1 &
    

    >output.log将输出重定向到日志文件, 2&>1将附加错误流到输出,有效地到日志文件。最后&将命令置于后台。请注意, nohup 命令将从终端组中分离进程。

    现在您可以安全地退出 ssh shell。因为你的脚本在终端组之外,所以它不会被杀死。它将从您的 shell 进程重新加入系统 INIT 进程。它类似于 Unix 系统行为。您可以使用命令监控的完整输出

    tail -f output.log   #allways breakable by ^C, it is only watching
    

    使用这种方法,您不需要使用 ^Z 、 bg 等 shell 技巧将命令置于后台。

Notice, using redirection to nohup command is preferred. Otherwise nohup will auto redirect all outputs for you to nohup.outfile in the current directory.

请注意,首选使用重定向到 nohup 命令。否则 nohup 将自动将所有输出重定向到nohup.out当前目录中的文件。

回答by Alex Howansky

You can use screen.

您可以使用screen.