title: Cron not running django command
body: <p>I have a django script that should be run at a specified time every day. I am trying to achieve this using <code>crontab</code>. The script is supposed to dump the database, archive it using <code>gzip</code> and upload it to <code>bitbucket</code>.</p>
  
  <p>The following is the relevant part of my <code>crontab</code> file:</p>
  
  <pre><code>00 4    * * *   root    python /my_django_project_path/manage.py update_locations
  47 16   * * *   root    python /my_django_project_path/manage.py database_bu
  </code></pre>
  
  <p>When I execute <code>python /my_django_project_path/manage.py database_bu</code> it works perfectly fine. However crontab either does not execute it or something happens along the way. Even weirder, the first crontab command (<code>update_locations</code>) is executed perfectly fine.</p>
  
  <p>Reading <a href="https://stackoverflow.com/questions/4519829/scheduling-django-custom-command">this question</a>, I have tried the following, without success:</p>
  
  <p><strong>Changing the command to:</strong></p>
  
  <pre><code>47 16   * * *   root    (cd /my_django_project_path/ &amp;&amp; python manage.py database_bu)
  </code></pre>
  
  <p><strong>Changing the command to:</strong></p>
  
  <pre><code>47 16   * * *   root    /usr/bin/python /my_django_project_path/manage.py database_bu
  </code></pre>
  
  <p><strong>Adding the following to my script (even though the other one works fine without it):</strong></p>
  
  <pre><code>#!/usr/bin/python
  
  from django.core.management import setup_environ
  import settings
  setup_environ(settings)
  </code></pre>
  
  <p><strong>Running everything through a script that exports the django project settings:</strong></p>
  
  <p>/my_django_project_path/cron_command_executor.sh:</p>
  
  <pre><code>export DJANGO_SETTINGS_MODULE=my_django_project.settings 
  python manage.py ${*}
  </code></pre>
  
  <p>The following in crontab:</p>
  
  <pre><code>47 16   * * *   root    ./my_django_project_path/cron_command_executor.sh database_bu
  </code></pre>
  
  <p><strong>Changing the user to both my user and the Apache user (<code>www-data</code>).</strong></p>
  
  <p><strong>I have a newline at the end of my crontab file.</strong></p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>When doing <code>sudo su</code>, running the command manually no longer works. It gets stuck and doesn't do anything.</p>
  
  <p>The output of <code>tail -f /var/log/syslog</code> is: </p>
  
  <pre><code>Mar 3 18:26:01 my-ip-address cron[726]: (system) RELOAD (/etc/crontab) 
  Mar 3 18:26:01 my-ip-address CRON[1184]: (root) CMD (python /my_django_project_path/manage.py database_bu)
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I am using the following <code>.netrc</code> file to prevent git asking for credentials:</p>
  
  <pre><code>machine bitbucket.org
      login myusername
      password mypassword
  </code></pre>
  
  <p>The actual code for the backup script is:</p>
  
  <pre><code>import subprocess
  import sh
  import datetime
  import gzip
  from django.core.management.base import BaseCommand
  
  class Command(BaseCommand):
      def handle(self, *args, **options):
          execute_backup()
  
  FILE_NAME = 'some_file_name.sql'
  ARCHIVE_NAME = 'some_archive_name.gz'
  REPO_NAME    = 'some_repo_name'
  GIT_USER = 'some_git_username' # You'll need to change this in .netrc as well.
  MYSQL_USER   = 'some_mysql_user'
  MYSQL_PASS   = 'some_mysql_pass'
  DATABASE_TO_DUMP = 'SomeDatabase' # You can use --all-databases but be careful with it! It will dump everything!.
  
  def dump_dbs_to_gzip():
      # Dump arguments.
      args = [
          'mysqldump', '-u', MYSQL_USER, '-p%s' % (MYSQL_PASS),
          '--add-drop-database',
          DATABASE_TO_DUMP,
      ]
      # Dump to file.
      dump_file = open(FILE_NAME, 'w')
      mysqldump_process = subprocess.Popen(args, stdout=dump_file)
      retcode = mysqldump_process.wait()
      dump_file.close()
      if retcode &gt; 0:
          print 'Back-up error'
      # Compress.
      sql_file = open(FILE_NAME, 'r')
      gz_file = gzip.open(ARCHIVE_NAME, 'wb')
      gz_file.writelines(sql_file)
      gz_file.close()
      sql_file.close()
      # Delete the original file.
      sh.rm('-f', FILE_NAME)
  
  def clone_repo():
      # Set the repository location.
      repo_origin = 'https://%s@bitbucket.org/%s/%s.git' % (GIT_USER, GIT_USER, REPO_NAME)
  
      # Clone the repository in the /tmp folder.
      sh.cd('/tmp')
      sh.rm('-rf', REPO_NAME)
      sh.git.clone(repo_origin)
      sh.cd(REPO_NAME)
  
  def commit_and_push():
      # Commit and push.
      sh.git.add('.')
      sh.git.commit(m=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
      sh.git.push('origin', 'master')
      sh.cd('..')
      sh.rm('-rf', REPO_NAME)
  
  def execute_backup():
      clone_repo()
      dump_dbs_to_gzip()
      commit_and_push()
  
  if __name__ == "__main__":
      execute_backup()
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I managed to fix it using <a href="https://stackoverflow.com/a/22303535/1195527">Chris Clark's suggestion of calling the script directly rather than through manage.py</a>. However, I am still interested in what is causing this issue so the bounty is still available.</p>
  
  <p><strong>UPDATE [SOLVED]:</strong></p>
  
  <p>Adding the following line to <code>/etc/environment</code> and running it as my user account rather than root fixed it:</p>
  
  <pre><code>PWD=/my_django_project_path/helpers/management/commands
  </code></pre>
  
  <p>I still wonder why only my user can run it so if anyone has the solution to that, please contribute.</p>