write python script that is executed every 5 minutes
Asked Answered
R

4

7

I need to write a python script that autostarts on boot and is executed every 5 minutes on a raspberry pi. How can this be done? in particular, how can I avoid having a script locking up the cpu running a infine loop waiting for the 5 minutes to be over?

Remake answered 8/2, 2014 at 16:14 Comment(1)
Use cron, but gevent will also be able to do the task nicely or sleepBatting
M
21

You can easily use cron for this task (schedule to run Python script). ;)

How to setup cron

I suppose that you have cron installed already; if not, then install some (vixie-cron for an example).

Create a new file /etc/cron.d/<any-name>.cron with the following content:

# run script every 5 minutes
*/5 * * * *   myuser  python /path/to/script.py

# run script after system (re)boot
@reboot       myuser  python /path/to/script.py

where myuser is the user to run the script (it shouldn’t be root if possible, for security reasons). If this doesn’t work, then try to append the content to /etc/crontab instead.

You might want to redirect stdout/stderr of the script to file, so you can check if everything works fine. This is same as in shell, just add something like >>/var/log/<any-name>-info.log 2>>/var/log/<any-name>-error.log after the script path.

Morelock answered 8/2, 2014 at 16:21 Comment(8)
thanks! do you have an example on how to set it up on raspbian?Remake
It depends on OS you have on your Raspberry, it’s irrelevant that it’s a Raspberry, not PC. However it’s basically the same on all Linux distros. I’ve updated my answer, hope it helps.Morelock
Ah, Raspbian is actually a distro, not some short for Raspberry Pi. :)Morelock
thanks! root is disabled anyway on raspbian.. :) what if i need to make a reboot through my script? it wont work with a different user..Remake
Then you just run sudo reboot from the script…?Morelock
great thanks! do i need to "start" the cron somehow? i created the file but its not doing anything.. also, i did not understand your sentence about /etc/crontabRemake
Of course, cron is a daemon, you should add it to runlevel to be started automatically after boot.Morelock
ive been googeling till now but cant seem to find how to add the daemon to autostart.. cant even find the name of the daemon...Remake
C
8

Use schedule

  • wrap the scrip in a function
import schedule 
import time 


def func():
    print("this is python")

schedule.every(5).minutes.do(func)

while True:
    schedule.run_pending()
    time.sleep(1)
Curtin answered 30/4, 2020 at 10:52 Comment(0)
M
3

You can use time.sleep

count = -1
while(not abort):
    count = (count+1) % 100
    if count == 0:
        print('hello world!')
    time.sleep(3)
Masse answered 8/2, 2014 at 16:20 Comment(0)
S
2

I am considering your code takes less than 5 minutes, but the execution time for each run is not constant.

import time

while True:
  t= time.time()

  # your code goes here
................
........
  
  t= time.time()-t
  time.sleep(300-t)
Swaddle answered 6/6, 2021 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.