Running apschduler in Python script as a daemon?

Go To


I have a which has the following code.

import datetime
import logging
import sys
import os

from apscheduler.scheduler import Scheduler
from src.extractors.pExtractor import somejob

def run_job():
    start =
    logging.debug('Proposal extraction job starting')
    end =
    duration = end - start
    logging.debug('job completed , took ' + str(duration.seconds) + ' seconds')

def main():
    logging.basicConfig(filename='/tmp/pExtractor.log', level=logging.DEBUG,format='%(levelname)s[%(asctime)s]: %(message)s')
    sched = Scheduler()
    sched.add_interval_job(run_job, minutes=2)

if __name__ == '__main__':
  • When I run this on the command prompt, it exits immediately:

INFO[2012-04-03 13:31:02,825]: Started thread pool with 0 core threads and 20 maximum threads INFO[2012-04-03 13:31:02,827]: Scheduler started INFO[2012-04-03 13:31:02,827]: Added job "run_job (trigger: cron[minute='2'], next run at: 2012-04-03 14:02:00)" to job store "default" INFO[2012-04-03 13:31:02,828]: Shutting down thread pool

  • How can I makde this run as a daemon?
2012-04-03 20:32
by daydreamer
The resoulution is here : - daydreamer 2012-04-03 20:50


Write your main() as below.

def main():
    [... your_code_as_in_your_question ...]
    while (True):

Additionally it shouldn't hurt to consider PEP 3143.

2012-10-02 20:50
by KAcper Perschke