使用 ssh 在 tmux 中运行的 Python 脚本在执行过程中挂起
我需要每天 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一:
正如@tripleee所说,确保你的代码完全运行
第二
当你说hanged时,这意味着你正在失去与ssh的连接?如果是这样,您可以使用
ssh
在远程启动另一个终端会话,打开tmux
,然后按ctrl+b+s
选择箭头并输入正在运行的旧会话,它仍然会运行。如果您知道会话的名称,还有另一种方法可以直接附加到您的会话。
选项b
使用
nohup
命令调用Python;例如:即使连接断开,这也会保留任务并将打印/日志存储在日志文件中。
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
, opentmux
, and pressctrl+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:This will persist the task even if the connection is broken and store the prints/logs in a log file.