Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

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

like image 818
user1800356 Avatar asked Jan 21 '26 05:01

user1800356


2 Answers

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
like image 145
Bill Avatar answered Jan 23 '26 03:01

Bill


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!

like image 31
njachowski Avatar answered Jan 23 '26 03:01

njachowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!