title: Cron job with django application
body: <p>I would like to use a <code>cron</code> task in order to delete <code>media files</code> if the condition is True.</p>
  
  <p>Users generate export files stored in the Media folder. In order to clean export files in the background, I have a Cron task which loops over each file and looks if the expiry delay is passed or not.</p>
  
  <p>I used <code>django-cron</code> library</p>
  
  <p><strong>Example:</strong></p>
  
  <p>File in Media Folder : <em>Final_Products___2019-04-01_17:50:43.487845.xlsx</em></p>
  
  <p>My Cron task looks like this :</p>
  
  <pre><code>class MyCronExportJob(CronJobBase):
      """ Cron job which removes expired files at 18:30 """
      RUN_AT_TIMES = ['18:30']
  
      schedule = Schedule(run_at_times=RUN_AT_TIMES)
      code = 'app.export_cron_job'
  
      def do(self):
          now = datetime.datetime.now()
          media_folder = os.listdir(os.path.join(settings.MEDIA_ROOT, 'exports'))
          for files in media_folder:
              file = os.path.splitext(files.split(settings.EXPORT_TITLE_SEPARATOR, 1)[1])[0]
              if datetime.datetime.strptime(file, '%Y-%m-%d_%H:%M:%S.%f') + timedelta(minutes=settings.EXPORT_TOKEN_DELAY) &lt; now:
                  os.remove(os.path.join(os.path.join(settings.MEDIA_ROOT, 'exports'), files))
  
  # settings.EXPORT_TOKEN_DELAY = 60 * 24
  </code></pre>
  
  <p><strong>I edited my crontab -e :</strong></p>
  
  <pre><code>30 18 * * * source /home/user/Bureau/Projets/app/venv/bin/activate.csh &amp;&amp; python /home/user/Bureau/Projets/app/src/manage.py runcrons --force app.cron.MyCronExportJob
  </code></pre>
  
  <p>Then I launched <code>service cron restart</code></p>
  
  <p>But nothing as changed. My file is still there. However, it should be removed because his date is greater than <code>now + settings.EXPORT_TOKEN_DELAY</code></p>
  
  <p>I'm using Ubuntu to local dev and FreeBSD as a production server environment.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I tried some things but crontab doesn't work for the moment.</p>
  
  <p>1) <code>* * * * * /bin/date &gt;&gt; /home/user/Bureau/Projets/app/cron_output</code>
  ==> It works, so crontab works</p>
  
  <p>2) I ran : <code>python manage.py runcrons</code> in my console
  ==> It works</p>
  
  <p>3) I ran this script (cron.sh):</p>
  
  <pre><code>source /home/user/.bashrc
  cd /home/user/Bureau/Projets/app
  pyenv activate app
  
  python src/manage.py runcrons --force
  
  deactivate
  </code></pre>
  
  <p>==> It works</p>
  
  <p>4) I ran this crontab line :</p>
  
  <pre><code>35 10 * * * /home/user/Bureau/Projets/app/utility/cron.sh
  </code></pre>
  
  <p>==> Service restarted at 10h32, I waited until 10h38 : nothing !</p>