1

So I want to run this command python run.py from the linux terminal every hour. What is the best way to do this?

5
  • 1
    did you look into cron? Commented Jun 27, 2017 at 12:03
  • 1
    cron .e.g stackoverflow.com/questions/26340358/set-up-a-cron-every-hour Commented Jun 27, 2017 at 12:03
  • superuser.com/questions/139401/… Commented Jun 27, 2017 at 12:06
  • 1
    Do you really need that script to be run in a terminal (read tty demystified to understand how complex a "terminal" is), or do you just want that script to be run every hour (without a terminal)? Please edit your question to improve it and motivate it (what is run.py doing, 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...) Commented Jun 27, 2017 at 12:08
  • Out of interest why a terminal rather than as a batchjob with a log file? Commented Jun 27, 2017 at 13:42

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

Good, however this wont run the script in a terminal but simply in a batch job
4

The simple way is using the cron job, using this command crontab -e you will see the image below enter image description here

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

3

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

1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.