title: python crontab alternative - APScheduler & python-daemon
body: <p>I'm having trouble using 
  <a href="http://pypi.python.org/pypi/python-daemon/1.6" rel="nofollow">python-daemon 1.6</a> getting along with APScheduler to manage a list of tasks.</p>
  
  <p>(The scheduler needs to run them  periodically at a specific chosen times - seconds resolution)</p>
  
  <p><strong>Working (until pressing Ctrl+C),</strong></p>
  
  <pre><code>from apscheduler.scheduler import Scheduler
  import logging
  import signal
  
  def job_function():
      print "Hello World"
  
  def init_schedule():
          logging.basicConfig(level=logging.DEBUG)
          sched = Scheduler()
          # Start the scheduler
          sched.start()
  
          return sched
  
  def schedule_job(sched, function, periodicity, start_time):
          sched.add_interval_job(job_function, seconds=periodicity, start_date=start_time)
  
  if __name__ == "__main__":
  
      sched = init_schedule()
      schedule_job(sched, job_function, 120, '2011-10-06 12:30:09')
      schedule_job(sched, job_function, 120, '2011-10-06 12:31:03')
  
      # APSScheduler.Scheduler only works until the main thread exits
      signal.pause()
      # Or
      #time.sleep(300)
  </code></pre>
  
  <p><strong>Sample Output:</strong></p>
  
  <p>INFO:apscheduler.threadpool:Started thread pool with 0 core threads and 20 maximum threads
  INFO:apscheduler.scheduler:Scheduler started
  DEBUG:apscheduler.scheduler:Looking for jobs to run
  DEBUG:apscheduler.scheduler:No jobs; waiting until a job is added
  INFO:apscheduler.scheduler:Added job "job_function (trigger: interval[0:00:30], next run at: 2011-10-06 18:30:39)" to job store "default"
  INFO:apscheduler.scheduler:Added job "job_function (trigger: interval[0:00:30], next run at: 2011-10-06 18:30:33)" to job store "default"
  DEBUG:apscheduler.scheduler:Looking for jobs to run
  DEBUG:apscheduler.scheduler:Next wakeup is due at 2011-10-06 18:30:33 (in 10.441128 seconds)</p>
  
  <p><strong>With python-daemon,</strong>
  Output is blank. Why isn't the DaemonContext correctly spawning the processes?</p>
  
  <p><strong>EDIT - Working</strong></p>
  
  <p>After reading python-daemon source, I've added stdout and stderr to the DaemonContext and finally was able to know what was going on.</p>
  
  <pre><code>def job_function():
      print "Hello World"
      print &gt;&gt; test_log, "Hello World"
  
  def init_schedule():
      logging.basicConfig(level=logging.DEBUG)
      sched = Scheduler()
      sched.start()
  
      return sched
  
  def schedule_job(sched, function, periodicity, start_time):
      sched.add_interval_job(job_function, seconds=periodicity, start_date=start_time)
  
  if __name__ == "__main__":
  
      test_log = open('daemon.log', 'w')
      daemon.DaemonContext.files_preserve = [test_log]
  
      try:
             with daemon.DaemonContext():
                     from datetime import datetime
                     from apscheduler.scheduler import Scheduler
                     import signal
  
                     logging.basicConfig(level=logging.DEBUG)
                     sched = init_schedule()
  
                     schedule_job(sched, job_function, 120, '2011-10-06 12:30:09')
                     schedule_job(sched, job_function, 120, '2011-10-06 12:31:03')
  
                     signal.pause()
  
      except Exception, e:
             print e
  </code></pre>