0

How to make cronjob for Django in Hosing, VPS or Server. This config also what we do in this site.

For example, in your file of /yourproject/yourapp/management/commands/autobackup.py

from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    help = 'To backup your app!'
    def add_arguments(self, parser):
        parser.add_argument(
            'backup',
            help='To backup your app!'
        )
    def handle(self, *args, **options):
        if options['backup'] == 'yes':
            # Do stuff
            self.stdout.write(self.style.SUCCESS('[+] Successfully backup!'))
        else:
            self.stdout.write(self.style.WARNING('[-] Can not backup!'))

If you work with hosting, you can setup on Cron Jobs, and setup your time with following this command:

source /path/to/yourenv/bin/activate && cd /path/to/yourenv/yourproject && ./manage.py autobackup yes

But, if you work with VPS or SERVER, please following this command bellow:

$ sudo crontab -e

and then edit it:

# Setup to daily method.
[minute] [hour] [date] [month] [year]
59 23 * * * source /path/to/yourenv/bin/activate && cd /path/to/yourenv/yourproject && ./manage.py autobackup yes

Refferences:

See also:

server linux django

Your Answer

blog comments powered by Disqus