简体   繁体   English

如何始终在 linux (ubuntu) 中运行 python 脚本

[英]How to run a python script always in linux (ubuntu)

I have a linux system server using ssh to connect.我有一个使用 ssh 连接的 linux 系统服务器。 Now I have a python script.I want it run always.I using this commond现在我有一个 python 脚本。我希望它始终运行。我使用这个公共

ubuntu:~$ nohup python3 -u ~/test/main.py > test.outs 2>&1 &

but I exit ssh connect,That python script exit at the same time.但是我退出ssh连接,那个python脚本同时退出。 What should I do?我该怎么办?

You could run the script regularly / check it is running with a cronjob and this would also allow you to run the script at system start-up so it would keep running in the event of a reboot.您可以定期运行脚本/检查它是否使用 cronjob 运行,这也将允许您在系统启动时运行脚本,以便在重新启动时继续运行。

There are a few suggestions here;这里有一些建议; https://superuser.com/questions/448445/run-bash-script-in-background-and-exit-terminal https://superuser.com/questions/448445/run-bash-script-in-background-and-exit-terminal

Although this also suggests that nohub should stop the child process being killed when you exit the session.虽然这也表明 nohub 应该在您退出会话时停止被杀死的子进程。 How are you aware that the script stops running upon exit?您如何知道脚本在退出时停止运行?

There are multiple ways to do this and they depend on your use-case.有多种方法可以做到这一点,它们取决于您的用例。

One way to do it is to install 'screen' on your server.一种方法是在您的服务器上安装“屏幕”。

sudo apt-get install screen

Now, whenever you connect to your server with ssh, you can type 'screen' and then start your code there.现在,无论何时您使用 ssh 连接到您的服务器,您都可以输入“screen”,然后在那里开始您的代码。 (first time you do this, you get an explanation on screen, press space to skip it) (第一次这样做时,你会在屏幕上看到一个解释,按空格键跳过它)

With this code executing, you type Ctrl + A and then Ctrl + D. This 'detaches' the screen.执行此代码后,您键入 Ctrl + A,然后按 Ctrl + D。这将“分离”屏幕。 You can now disconnect and this 'screen' will keep existing and the code will still run.您现在可以断开连接,此“屏幕”将保持存在并且代码仍将运行。

When reconnecting, you might want to go back to this screen.重新连接时,您可能希望返回此屏幕。 Type类型

screen -ls

to get an overview of screens that you have running.以获得您正在运行的屏幕的概览。 (in this case, there'll only be one) They can be identified by the 5 numbers they all start with. (在这种情况下,将只有一个)它们可以通过它们全部开头的 5 个数字来识别。 So go back to this screen by typing因此,通过键入返回此屏幕

screen -r XXXXX

and now you're back.现在你回来了。 More information on this here:此处的更多信息:

https://www.howtogeek.com/662422/how-to-use-linuxs-screen-command/ https://www.howtogeek.com/662422/how-to-use-linuxs-screen-command/

Again, I don't know what your script does so it might not be the best solution.同样,我不知道你的脚本是做什么的,所以它可能不是最好的解决方案。

You may use screen您可以使用屏幕

After installation, you can start screen by sending screen to the console, and then run your script.安装后,您可以通过将screen发送到控制台来启动屏幕,然后运行您的脚本。 When you type CTRL+A+D , the screen will disappear and you can exit your ssh connection.当您键入CTRL+A+D ,屏幕将消失,您可以退出 ssh 连接。

If you re-open the screen you opened before, just type screen -r如果重新打开之前打开的屏幕,只需键入screen -r

If you have multiple screen instances, the app will show you numbers of screen when you type screen -r .如果您有多个屏幕实例,当您键入screen -r时,应用程序将显示屏幕数量。 You just need to find the id of the screen and type screen -r id ie screen -r 2643你只需要找到屏幕的 id 并输入screen -r id ie screen -r 2643

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

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