Getting cronjob to work from Django

I have a droplet with Django installed and I can run a script from /home/sammy/revamp like python getimages it returns output like

Successfully found image for "6c90a52a75c5a91054937d6ab3c8cd4e.jpg"

Problem is it never runs itself from crontab -e

*/1 * * * * sammy cd /home/sammy/revamp/ && /usr/bin/python getimages >> /var/log/getimages.log

This is the last of many editing I’ve done to this log.

Btw here’s it’s code

class Command(BaseCommand):
    help = 'Update images coloured to database'

    def handle(self, *args, **options):
        #get filenames automatically
            images = listdir('/home/sammy/revamp/revamp/media/new_colour')

            for image_name in images:
                exists = Image.objects.filter(original_image='original/'+image_name)

                if exists:
                    image = exists[0]
                    image.colored_image = File( open('/home/sammy/revamp/revamp/media/new_colour/' + image_name, 'r'))
                    os.remove('/home/sammy/revamp/revamp/media/new_colour/' + image_name)

                    self.stdout.write('Successfully found image for "%s"' % image_name))

        except Exception, e:
            error_log = open('/home/sammy/revamp/error.log','a')
            error.write(str(e) + '\n')

It should output something to error.log but also doesn’t do this.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @muirurisamuel

I’m not sure if you login to the console with root or the user sammy, but try running this command to make sure you’re setting the cronjob for the specific user:

sudo crontab -u sammy -e

Then change the cronjob to this:

*/1 * * * * /usr/bin/python /home/sammy/revamp/ getimages >> /var/log/getimages.log

And you can see the error log of the cron service by looking in the syslog log:

cat /var/log/syslog | grep "CRON"