samy
samy

Reputation: 21

Python sched scheduler and reboots

I have read about python sched (task scheduler), it works like a cron.

but I have a question :

Does the scheduler automatically start and run the function after 2 hours? Or do I have to start that again after shutting down the system?

Upvotes: 0

Views: 1717

Answers (2)

samy
samy

Reputation: 21

and if this is so :
will this also work even after system restart?
answer is : NO then how can turbogear scheduler can run using cronos in cron? scheduled events in turbogear will also be vanished after SYSTEM restart.
PLEASE CORRECT ME IF I AM WRONG.

import time
import sched
import datetime
import threading
import calendar
#from datetime import datetime


class test:

    def __init__(self):
        self.name = ''

    def getSec(self):

        now = datetime.datetime.now()
        print "now - ", now
        currentYear = now.year
        currentMonth = now.month
        currentDay = now.day
        currentHour = now.hour
        currentMinute = now.minute
        currentSecond = now.second
        currentMicroseconds = now.microsecond
        command = "python runbackup.py"
        print "command is - ", command

        print "currentMinute - ", currentMinute
        print "currentSecond - ", currentSecond
        # current time
        a = datetime.datetime(currentYear, currentMonth, currentDay, currentHour, currentMinute, currentSecond, currentMicroseconds)

        last_date_of_current_month = calendar.monthrange(currentYear, currentMonth)[1]
        print "last_date_of_current_month - ", last_date_of_current_month
        b = datetime.datetime(currentYear, currentMonth, int(last_date_of_current_month), 23, 59, 59, 000000)
        #b = datetime.datetime(currentYear, currentMonth, int(29), 18, 29, 00, 000000)
        #print "date time of b is - %s %s " % (18, 29)

        c = b-a
        print "c is - ", c

        time.sleep(1)
        scheduler = sched.scheduler(time.time, time.sleep)
        #scheduler.cancel(e1)
        sec = c.seconds
        print "second -  ", sec
        print "scheduler entered."
        e1 = scheduler.enter(sec, 1, self.getSec, ())
        t = threading.Thread(target=scheduler.run)
        print "thread started."
        print "======================================"
        t.start()

        #scheduler.cancel(e1)
        #print "canceled."

        return True

if __name__=='__main__'  :
    obj = test()
    obj.getSec()

Upvotes: 0

Senthil Kumaran
Senthil Kumaran

Reputation: 56871

Answer to all three questions is No.

sched is different from cron. It takes a generic timer or counter function and a delay function and lets you to schedule a function call after a particular time (an event as defined by your generic timer function).

It won't run after you close your program, unless you maintain state by writing to a file or db. This is complicated and using cron would be better.

sched works on events, but not on background. so, it not is not exactly a deamon, but you can deamonize it running the program in the background using OS facilities.

Upvotes: 1

Related Questions