The Python Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 How To Run A Python Script At A Certain Time In A Tmux Terminal?
00:31 Answer 1 Score 1
00:45 Answer 2 Score 1
01:46 Answer 3 Score 0
02:04 Accepted Answer Score 1
02:23 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