使用 ssh 在 tmux 中运行的 Python 脚本在执行过程中挂起

发布于 2025-01-16 22:41:31 字数 1152 浏览 1 评论 0原文

我需要每天 8 小时,每分钟执行一些操作。我计划使用 cron 作业启动 python 脚本,然后使用日期时间和睡眠函数,我尝试实现每分钟执行并执行到某个时间。

我通过 ssh 连接到远程 Linux 虚拟机,使用 tmux 打开会话,然后运行 ​​python 脚本。 开始时间是中午12点37分。 以下是代码::

import datetime
from time import sleep
print(datetime.datetime.now().time())
min = datetime.datetime.now().minute
i = 0
while(True):
    if (min == datetime.datetime.now().minute):
        i = i + 1
        print("conditions check " + str(i) + " timestamp :: " + str(datetime.datetime.now().time()) )
        print("conditions check end " + str(i) + " timestamp :: " + str(datetime.datetime.now().time()) )
        sleep(60 - datetime.datetime.now().second)
    elif(min < datetime.datetime.now().minute):
        min = datetime.datetime.now().minute
    if(datetime.datetime.now().time() > datetime.time(17,55,0)):
        print(datetime.datetime.now().time())
        break

执行了 i = 22 即 22 分钟,然后它停止工作并被挂起。

我经常遇到无法在 tmux 会话中向终端输入文本的情况。

ps aux 的输出 | grep 蟒蛇

PID     %CPU    %MEM     VSZ       RSS   TTY      STAT        COMMAND
21495     85.9  0.5     123808    5364   pts/3    R+      python test1.py

I have a requirement to perform some action every minute for 8 hours a day. I have a plan to start the python script using a cron job and then using datetime and sleep functions I try to achieve every minute execution and execution till some time.

I ssh to a remote linux vm, use tmux to open a session and then run the python script.
Start time was 12:37 pm.
Following is the code ::

import datetime
from time import sleep
print(datetime.datetime.now().time())
min = datetime.datetime.now().minute
i = 0
while(True):
    if (min == datetime.datetime.now().minute):
        i = i + 1
        print("conditions check " + str(i) + " timestamp :: " + str(datetime.datetime.now().time()) )
        print("conditions check end " + str(i) + " timestamp :: " + str(datetime.datetime.now().time()) )
        sleep(60 - datetime.datetime.now().second)
    elif(min < datetime.datetime.now().minute):
        min = datetime.datetime.now().minute
    if(datetime.datetime.now().time() > datetime.time(17,55,0)):
        print(datetime.datetime.now().time())
        break

This executed for i = 22 i.e 22 minutes and then it stopped working and got hanged.

I frequently experience not able to input text to terminal in tmux sessions.

output of ps aux | grep python

PID     %CPU    %MEM     VSZ       RSS   TTY      STAT        COMMAND
21495     85.9  0.5     123808    5364   pts/3    R+      python test1.py

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2025-01-23 22:41:31

第一:

正如@tripleee所说,确保你的代码完全运行

第二

当你说hanged时,这意味着你正在失去与ssh的连接?如果是这样,您可以使用 ssh 在远程启动另一个终端会话,打开 tmux,然后按 ctrl+b+s 选择箭头并输入正在运行的旧会话,它仍然会运行。

如果您知道会话的名称,还有另一种方法可以直接附加到您的会话。

选项b

使用nohup命令调用Python;例如:

nohup python3 main.py >> log.file &&

即使连接断开,这也会保留任务并将打印/日志存储在日志文件中。

First:

As @tripleee says, ensure your code runs at all

Second

When you say hanged it means that you are losing the connection to ssh? If so, you can start another terminal session enter in your remote using ssh, open tmux, and press ctrl+b+s to select with the arrows and enter your old session that was working, it will still be working.

There are another ways to attach directly for your session if you know the name of the session.

Option b

Call Python using the nohup command; for example:

nohup python3 main.py >> log.file &&

This will persist the task even if the connection is broken and store the prints/logs in a log file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文