简体   繁体   English

Google Cloud Shell脚本问题

[英]Google Cloud Shell scripting Issue

I am running my code from the shell(SSH) in google cloud as 我正在从谷歌云中的外壳程序(SSH)运行我的代码

python3 mycode.py python3 mycode.py

If I close the shell, the computation stops. 如果关闭外壳,计算将停止。 How can I start a computation and then close the shell(Computation takes a long Time:)).....come back later and see how it is doing. 我如何开始计算然后关闭外壳程序(计算需要很长时间:).....稍后再回来看看其运行情况。

My code keeps printing results after a certain number of iteration. 我的代码在经过一定次数的迭代后仍保持打印结果。

Well, in general what you can do is run the code in a way where you can detach from the interactive environment. 好吧,总的来说,您可以做的是以一种可以脱离交互式环境的方式运行代码。 Using a tool such as screen or tmux . 使用诸如screentmux的工具。 However, Google Cloud Shell is not made for running background tasks, and if i recall correctly, it will terminate after an hour. 但是,Google Cloud Shell并非用于运行后台任务,如果我没记错的话,它会在一小时后终止。

You might need to provision a virtual machine to run it on instead. 您可能需要配置一个虚拟机来运行它。 I can recommend using tmux. 我可以建议使用tmux。 With tmux, it will be as simple as running tmux and then in the new shell running your script python3 mycode.py . 使用tmux,它就像运行tmux一样简单,然后在新的shell中运行脚本python3 mycode.py You can then detach using ctrl+bd or simply disconnect. 然后,您可以使用ctrl+bd进行分离,也可以简单地断开连接。 When you reconnect you run tmux attach -d to get back to your script. 重新连接时,运行tmux attach -d返回到脚本。

I will provide you as well the following possibility: 我还将为您提供以下可能性:

A Bash Builtin command that you could also use is disown . 您也可以使用的Bash Builtin命令disown First, run your process/script in the background (using & , or stopping it with ^Z and then restarting with bg ): 首先,在后台运行您的进程/脚本(使用& ,或使用^Z停止它,然后使用bg重新启动):

$ long_operation_command &
[1] 1156

Note that at this point the process is still linked to the session and in case it is closed it will be killed. 请注意,此时该进程仍链接到该会话,并且如果该会话关闭,它将被杀死。 You can the process attached to the session check running jobs in the background: 您可以在会话中附加的进程在后台检查正在运行的jobs

$ jobs
[1]+  Running  long_operation_command

Therefore you can run disown in order to detach the processes from the session: 因此,您可以运行disown以便从会话中分离进程:

$ disown

You can confirm this checking the result of your script or command logging in again or checking with top the process still running. 您可以证实这一点再次检查你的脚本或命令日志记录的结果或与检查top的过程仍在运行。

Check also this because it could be interesting, ie the difference between nohup foo , foo & and $ foo & disown 也请检查此内容,因为它可能很有趣,即nohup foofoo &$ foo & disown之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM