So I want to run this command python run.py from the linux terminal every hour. What is the best way to do this?
4 Answers
Edit your crontab using
crontab -e
add the following line to run your script every hour
0 * * * * python <path-to-file>
you can list scheduled crons using crontab -l
1 Comment
The simple way is using the cron job, using this command
crontab -e you will see the image below

you can add this command to the cron configuration:
* */1 * * * python /root/yourprogram.py > /dev/null 2>&1
the */1 is for executing every hour the python program, look the structure of the cron command:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * * /usr/bin/find
Comments
Use the command watch on unix to run any command any set interval of time.
More information: https://en.wikipedia.org/wiki/Watch_(Unix)
(chose this way over cron because you specified in a terminal, and this would allow you to see the output in the terminal you start it from)
Comments
I would suggest you to use BlockingScheduler from apscheduler.schedulers.blocking.
Just install it using the command pip install APScheduler or pip3 install APScheduler. This is good.
from apscheduler.schedulers.blocking import BlockingScheduler
def your_job():
print("Do you job")
scheduler = BlockingScheduler()
scheduler.add_job(your_job, 'interval', seconds=5)
scheduler.start()
After every 5 seconds,
Do you job
Do you job
Will be printed. Great thing is you can also specify the minutes or hours just change the parameter. So in your case just change seconds=5 to hours=1.
from apscheduler.schedulers.blocking import BlockingScheduler
def your_job():
print("Do you job")
scheduler = BlockingScheduler()
scheduler.add_job(your_job, 'interval', hours=1)
scheduler.start()
run.pydoing, where exactly is it)? You say "the linux terminal" but in general you might have several (pseudo-)terminals, and you probably don't have any physical terminal (à la VT100, in museums...)