Search code examples
djangocron

Django cronjob not running


I am trying to create a cronjob from a custom Django command. When I run the command manually, it works perfect. Exactly what I want. The only thing is, that I want it scheduled (twice a day). But when I put the exact same command in crontab -e it doesn't work? Any suggestions?

Crontab -e:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
* * * * * /usr/bin/python3 /home/marvin/VulnScanner/manage.py pentest 3

Running just this command works fine:

/usr/bin/python3 /home/marvin/VulnScanner/manage.py pentest 3

Solution

  • Ok, I've found the solution. The crontab was actually running (as I tought), because te log said so.

    But I was using a executable in my python file, which crontab could not find. This had to do something with that crontab doesn't have PATH by default. The user shell does look in path for the executable. Crontab doesn't. You can fix this by adding at the top of your crontab:

    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    

    I've appended this to my cronjob, to get the output of my python script:

     >> /home/user/cronlog.log 2>&1