Reputation: 149
So I want to run this command python run.py from the linux terminal every hour. What is the best way to do this?
Upvotes: 1
Views: 11965
Reputation: 955
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
Upvotes: 4
Reputation: 2652
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()
Upvotes: 1
Reputation: 1653
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
Upvotes: 8
Reputation: 140
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)
Upvotes: 3