如何在tmux终端中的某个时间运行python脚本?

时间:2022-02-11 08:04:34

I have a python script, or should I say a python service which needs to run every day at 3pm. Basically, there is a while True : time.sleep(1) at the end of the file.

我有一个python脚本,或者我应该说一个需要每天下午3点运行的python服务。基本上,文件末尾有一段时间为True:time.sleep(1)。

I absolutely need this script to execute in a terminal window (because I need the logs). Bonus if the solution makes it possible to run in a tmux window.

我绝对需要这个脚本在终端窗口中执行(因为我需要日志)。如果解决方案可以在tmux窗口中运行,则可获得奖励。

I tried cron jobs but can't figure out how to put this in a terminal.

我试过cron的工作,但无法弄清楚如何把它放在终端。

4 个解决方案

#1


1  

I'd suggest sticking with cron and using the tee command to log the results. Your crontab would look something like this:

我建议坚持使用cron并使用tee命令记录结果。你的crontab看起来像这样:

0 15 * * *     python yourScript.py | tee –a logFile.txt

#2


1  

Okay, I see what you're saying. I've done some similar stuff in the past.

好的,我明白你在说什么。我过去做过类似的事情。

For the cron to run your script at 3pm and append to a log file you can do that simply like this:

为了让cron在下午3点运行你的脚本并附加到日志文件,你可以这样做:

0 15 * * * command >> log # just logs stdout

0 15 * * *命令>> log#只记录stdout

or

0 15 * * * command &>> log # logs both stdout and stderr

0 15 * * *命令&>> log#记录stdout和stderr

If you want it in the terminal I can think of two possibilities:

如果你想在终端中,我可以想到两种可能性:

  • Like you said, you could do a while true loop that checks the time every n seconds and when it's 3pm do something.

    就像你说的那样,你可以做一个真正的循环,每隔n秒检查一次,下午3点做一些事情。

  • Alternately you could set up an API endpoint that's always on and trigger it by some other program at 3pm. This could be triggered by the cron for example.

    或者,您可以设置一个始终打开的API端点,并在下午3点由其他程序触发它。例如,这可以由cron触发。

Personally I also like the convenience of having a tmux or screen to login to to see what's been happening rather than just checking a log file. So I hope you figure out a workable solution for your use case!

就个人而言,我也喜欢使用tmux或屏幕登录以查看正在发生的事情,而不仅仅是检查日志文件。所以我希望你能为你的用例找到一个可行的解决方案!

#3


0  

As mentioned above cronjob that redirects output to a file might be the simplest but if that is not possible an you want to keep process running you can set timeout till next day 3pm.

如上所述,将输出重定向到文件的cronjob可能是最简单的,但如果这是不可能的,您希望保持进程运行,您可以设置超时直到第二天下午3点。

h_exec = 15
t_hour = 60 * 60
t_day = 24 * t_hour
t_now = time.time()
t_timeout = math.floor((t_now + ((24 - h_exec) * t_hour)) / t_day) * t_day + (h_exec * t_hour) - t_now

#4


0  

thanks all for the answers. I used a cron job with a bash script for TMUX Scripting.

谢谢大家的答案。我使用了一个带有用于TMUX Scripting的bash脚本的cron作业。

Something like that :

像这样的东西:

#!/bin/bash
tmux new-session -d -s example
tmux split-window -h
tmux select-pane -t 0
tmux send-keys './ex.sh' 'C-m'
tmux select-pane -t 1
tmux send-keys './ex1.sh'
tmux new-window -n 'a'
tmux send-keys 'cd a' 'C-m'
tmux select-window -t "example:0"
tmux select-pane -t 0
tmux -2 attach-session -t example

#1


1  

I'd suggest sticking with cron and using the tee command to log the results. Your crontab would look something like this:

我建议坚持使用cron并使用tee命令记录结果。你的crontab看起来像这样:

0 15 * * *     python yourScript.py | tee –a logFile.txt

#2


1  

Okay, I see what you're saying. I've done some similar stuff in the past.

好的,我明白你在说什么。我过去做过类似的事情。

For the cron to run your script at 3pm and append to a log file you can do that simply like this:

为了让cron在下午3点运行你的脚本并附加到日志文件,你可以这样做:

0 15 * * * command >> log # just logs stdout

0 15 * * *命令>> log#只记录stdout

or

0 15 * * * command &>> log # logs both stdout and stderr

0 15 * * *命令&>> log#记录stdout和stderr

If you want it in the terminal I can think of two possibilities:

如果你想在终端中,我可以想到两种可能性:

  • Like you said, you could do a while true loop that checks the time every n seconds and when it's 3pm do something.

    就像你说的那样,你可以做一个真正的循环,每隔n秒检查一次,下午3点做一些事情。

  • Alternately you could set up an API endpoint that's always on and trigger it by some other program at 3pm. This could be triggered by the cron for example.

    或者,您可以设置一个始终打开的API端点,并在下午3点由其他程序触发它。例如,这可以由cron触发。

Personally I also like the convenience of having a tmux or screen to login to to see what's been happening rather than just checking a log file. So I hope you figure out a workable solution for your use case!

就个人而言,我也喜欢使用tmux或屏幕登录以查看正在发生的事情,而不仅仅是检查日志文件。所以我希望你能为你的用例找到一个可行的解决方案!

#3


0  

As mentioned above cronjob that redirects output to a file might be the simplest but if that is not possible an you want to keep process running you can set timeout till next day 3pm.

如上所述,将输出重定向到文件的cronjob可能是最简单的,但如果这是不可能的,您希望保持进程运行,您可以设置超时直到第二天下午3点。

h_exec = 15
t_hour = 60 * 60
t_day = 24 * t_hour
t_now = time.time()
t_timeout = math.floor((t_now + ((24 - h_exec) * t_hour)) / t_day) * t_day + (h_exec * t_hour) - t_now

#4


0  

thanks all for the answers. I used a cron job with a bash script for TMUX Scripting.

谢谢大家的答案。我使用了一个带有用于TMUX Scripting的bash脚本的cron作业。

Something like that :

像这样的东西:

#!/bin/bash
tmux new-session -d -s example
tmux split-window -h
tmux select-pane -t 0
tmux send-keys './ex.sh' 'C-m'
tmux select-pane -t 1
tmux send-keys './ex1.sh'
tmux new-window -n 'a'
tmux send-keys 'cd a' 'C-m'
tmux select-window -t "example:0"
tmux select-pane -t 0
tmux -2 attach-session -t example