The Python Oracle

How to run a python script at a certain time in a tmux terminal?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: The World Wide Mind

--

Chapters
00:00 Question
00:39 Accepted answer (Score 1)
01:02 Answer 2 (Score 1)
02:14 Answer 3 (Score 1)
02:33 Answer 4 (Score 0)
02:57 Thank you

--

Full question
https://stackoverflow.com/questions/3479...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #terminal #cron #tmux

#avk47



ANSWER 1

Score 1


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

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



ANSWER 2

Score 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:

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

or

0 15 * * * command &>> log # logs both stdout and 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.

  • 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.

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!




ACCEPTED ANSWER

Score 1


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

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



ANSWER 4

Score 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.

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