在Google Cloud Compute Engine上运行python脚本 [英] Run python script on Google Cloud Compute Engine

查看:85
本文介绍了在Google Cloud Compute Engine上运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是此的精确副本一个>问题,但是一段时间以来我一直在尝试不同的解决方案,但没有提出任何建议.

I know this is an exact copy of this question, but I've been trying different solutions for a while and didn't come up with anything.

我有一个使用PRAW在Reddit上查找帖子的简单脚本.这需要一段时间,所以当我退出shell时,我还需要它保持生命.

I have this simple script that uses PRAW to find posts on Reddit. It takes a while, so I need it to stay alive when I log out of the shell as well.

我试图将其设置为启动脚本,以使用nohup以便在后台运行它,但是这些都不起作用.我遵循了 quickstart ,这样我就可以运行hello word应用了,但是所有这些示例都是针对Web应用程序的,我想要的只是在我的VM上启动一个进程,并在未连接时使其保持运行,而无需使用.yaml配置文件等.有人可以指出我正确的方向吗?

I tried to set it up as a start-up script, to use nohup in order to run it in the background, but none of this worked. I followed the quickstart and I can get the hello word app to run, but all these examples are for web applications and all I want is start a process on my VM and keep it running when I'm not connected, without using .yaml configuration files and such. Can somebody please point me in the right direction?

推荐答案

嗯,最后使用nohup是答案.我是GNU环境的新手,我只是以为它在我初次尝试时不起作用.

Well, at the end using nohup was the answer. I'm new to the GNU environment and I just assumed it didn't work when I first tried. My program was exiting with an error, but I didn't check the nohup.out file so I was unaware of it..

无论如何,这里是一份详细的指南,供以后参考(使用Debian Stretch):

Anyway here is a detailed guide for future reference (Using Debian Stretch):

  1. 使脚本成为可执行文件

  1. Make your script an executable

chmod +x myscript.py

  • 运行nohup命令以在后台执行脚本. &选项可确保退出后该进程保持活动状态.我已将shebang行添加到我的python脚本中,因此无需在此处调用python

  • Run the nohup command to execute the script in the background. The & option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to call python here

    nohup /path/to/script/myscript.py &
    

  • 如果需要,可以从shell中注销

  • Logout from the shell if you want

    logout
    

  • 完成!现在您的脚本已启动并正在运行.您可以重新登录并通过检查以下命令的输出来确保您的进程仍然存在:

    Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:

    ps -e | grep myscript.py
    

    这篇关于在Google Cloud Compute Engine上运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆