pythonscriptingscheduled-tasks

Self scheduled Python script


I'm writing an Python script that calls a function that exports and imports spaces from the wiki tool Confluence. It has to run everyday, but we can't use cron so i'm looking for a way to schedule it by her self.

I've created the following. I can already schedule it for the next day but not for the following day again.

#!/usr/bin/python
from __future__ import print_function
from sys import argv
from datetime import datetime
from threading import Timer
import sys,os,subprocess
import getpass
from subprocess import PIPE,Popen

date = (os.popen("date +\"%d-%m-%y - %T\""))
x=datetime.today()
y=x.replace(day=x.day+1, hour=13, minute=56, second=0, microsecond=0)
delta_t=y-x
secs=delta_t.seconds+1


def runExport():
   # HERE IS ALL THE CODE THAT HAS TO RUN EVERYDAY

t = Timer(secs, runExport)
t.start()

Could somebody please help me out? The script has to run everyday for example an 05.00 am.

Version of Python is 2.6.6. And sadly enough no option to import a module.


Solution

  • I would suggest using something a bit simplilar then what you are doing there. There is a simple scheduling library in python called sched, which can be found here: https://docs.python.org/2/library/sched.html

    A simple example of using this for your implement:

    import sched, time
    s = sched.scheduler(time.time, time.sleep)
    delay_seconds = 5
    def print_time():
        print time.time()
        s.enter(delay_seconds,1,print_time,argument=())
    
    s.enter(delay_seconds,1,print_time,argument=())
    s.run()
    

    This code will run every 5 seconds, and print out the current time. just change delay_seconds to the delay you want your interval to run, and your done.