Cron does not run django command

I have a django script that needs to be run at a specific time every day. I am trying to achieve this using crontab. The script is supposed to delete the database, archive it with gzipand load it into bitbucket.

Below is the relevant part of my file crontab:

00 4    * * *   root    python /my_django_project_path/manage.py update_locations
47 16   * * *   root    python /my_django_project_path/manage.py database_bu

When I execute python /my_django_project_path/manage.py database_bu, it works great. However, crontab either does not execute it, or something happens along the way. Even stranger, the first crontab ( update_locations) command runs fine.

Reading this question , I tried the following, without success:

Change command to:

47 16   * * *   root    (cd /my_django_project_path/ && python manage.py database_bu)

Change command to:

47 16   * * *   root    /usr/bin/python /my_django_project_path/manage.py database_bu

Adding the following to my script (even if it works fine without it):

#!/usr/bin/python

from django.core.management import setup_environ
import settings
setup_environ(settings)

script, django:

/my_django_project_path/cron_command_executor.sh:

export DJANGO_SETTINGS_MODULE=my_django_project.settings 
python manage.py ${*}

crontab :

47 16   * * *   root    ./my_django_project_path/cron_command_executor.sh database_bu

, Apache (www-data).

crontab.

UPDATE:

sudo su . .

tail -f /var/log/syslog:

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)

UPDATE:

.netrc , git :

machine bitbucket.org
    login myusername
    password mypassword

script:

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 > 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()

UPDATE:

, Chris Clark script , manage.py. , , , - .

UPDATE [SOLVED]:

/etc/environment , root, :

PWD=/my_django_project_path/helpers/management/commands

, , , , , .

+4
4

- python /my_django_project_path/manage.py database_bu, , cron environment , cron, script ( ).

-, script

47 16 * * * root python/my_django_project_path/manage.py database_bu

root, , , . , root sudo su, , root . FWIW, - root , .

, cron .

47 16 * * * cd /my_django_project_path/ && python manage.py database_bu

cron. 2- : , cron, .netrc , , .

, , PATH , echo $PATH , /some/path:/some/other/path:/more/path/values, cron,

47 16 * * * export PATH="/some/path:/some/other/path:/more/path/values" && cd /my_django_project_path/ && python manage.py database_bu

, .

printenv > ~/environment.txt , , . cron * * * * * printenv > ~/cron_environment.txt, cron. , script, script

import os
os.system("printenv")

, , (, HOME), script/cron, , .

, , bitbucket .netrc, . .netrc cron.

ssh keypair ssh https ( , ssh- , ssh-keys.

, ssh, , , url .git/config ( origin_ssh git remote add origin_ssh url ssh).

, https URL- https://user@bitbucket.org/user/repo.git, ssh - git@bitbucket.org:user/repo.git.

PS: bitbucket, , , git , , . , (* * * * *) .

Edit

OP , PWD .

PWD =/my_django_project_path/helpers/management/commands /etc/environment

, , , , cron.

, , cron.

, .netrc , - ( sudo root), .

+2

. crontab? crontab:

... cron , crontab . crontab , cron crontab ( ) .

+2

- cron. , , python, manage.py, .

+2

Im strace, , , , , git . BitBucket, heres : git ssh remote; , ssh-agent ; root, theres no ssh-agent, git ssh .

git sudo su .

If this does not help, you need to get the output git(or what you actually call there). In the documentation for the package,sh specify how to redirect standard output and standard error.

0
source

Source: https://habr.com/ru/post/1529803/


All Articles