如何将 ssh 作业发送到后台
我通过 ssh 登录到远程服务器并启动了一个 php 脚本。看起来需要 17 个小时才能完成,有没有办法断开连接但保持脚本执行?我没有进行任何输出重定向,所以我看到了所有输出。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
会暂停它。然后输入
将其发送到后台。记下进程的PID以供以后使用;)
编辑:我忘了,之后你必须执行
其中$PID是你的进程的pid
,并且在你关闭终端后该进程不会被杀死。
will pause it. Than type
to send it to background. Write down the PID of the process for later usage ;)
EDIT: I forgot, you have to execute
where $PID is the pid of your process
after that, and the process will not be killed after you close the terminal.
您描述了保护脚本连续性很重要。不幸的是我不知道,您与脚本进行任何交互,而脚本是由您进行的。
如果您不需要操作员与脚本交互,您只需在开始时将脚本置于后台,并将完整的输出记录到日志文件中。只需使用命令:
>output.log 将把输出重定向到日志文件,2>1 将把错误流附加到输出中,有效地附加到日志文件中。最后一个 & 会将命令置于后台。请注意,nohup 命令将从终端组中分离进程。
现在您可以安全地退出 ssh shell。因为你的脚本不在终端组中,所以它不会被杀死。它将从您的 shell 进程重新加入系统 INIT 进程。它是类似 Unix 的系统行为。您可以使用命令监视完整的输出
使用此方法,您不需要使用 ^Z 、 bg 等 shell 技巧将命令置于后台。
请注意,最好使用重定向到 nohup 命令。否则,nohup 会自动将所有输出重定向到当前目录中的
nohup.out
文件。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.
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:
>output.log will redirect output into log file, 2&>1 will 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
Using this method you do not need use ^Z , bg etc shell tricks for putting command to the background.
Notice, using redirection to nohup command is preferred. Otherwise nohup will auto redirect all outputs for you to
nohup.out
file in the current directory.您可以使用
屏幕
。You can use
screen
.您现在可以停止该过程吗?如果是这样,请启动 screen,启动进程并使用 ctrl-a 然后 ctrl-d 分离屏幕。稍后使用 screen -r 检索会话。
这应该在大多数发行版中都可用,否则,肯定会有一个软件包可供您使用。
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.
This should be available in most distros, failing that, a package will definitely be available for you.