Setup a Cron Job

Hello,
I have been trying to make this cron job work but no luck so far.

crontab -e
*/5 * * * * source /home/ubuntu/.bashrc && source /home/ubuntu/work/your-project/bin/activate && python /home/ubuntu/work/your-project/src/manage.py runcrons > /home/ubuntu/cronjob.log

This is what the cron looks like but it seems not to be running.
It is a django project.

5 Replies

I'm sorry to hear that you're having trouble with this cron job. Based on the output above, it looks like you are using the syntax from this Django installation article. Without more information on the additional steps you took besides creating this cron, it's difficult to know where the issue may be occurring. Have gone through the additional steps in the installation guide?

It mentions:

installing django_cron
runing python manage.py migrate django_cron
extending the CronJobBase

If this has been completed, running the below command should cause the required cron jobs to to run.

python manage.py runcrons

You will also want to specify the name of your project, unless you named the directory your-project.

/home/ubuntu/work/your-project/bin/activate && python

If you still need some help after checking the things I've mentioned here, I recommend asking on the Django forums for help from the Django community.

Hi @rdaniels

I have the package working perfectly, it works if I run my commands manually, the problem I have is with the cron not running the command.

I have the project name and everything specified but the cron still won't work.

My cron job is exactly below.

5 * * * * source /home/joejoe/.bashrc && source /home/joejoe/web/django-site/virtual/bin/activate && python /home/joejoe/web/django-site/manage.py runcrons > /home/joejoe/cronjob.log >/dev/null

So, the problem is not the package or my setup, it is the cron job not running.
if I manually do

python manage.py runcrons

the cron task will run perfectly, I just need to automate the above command with the cron job.

5 * * * * …

Just a quick observation: you’re not expecting this to run every 5 minutes are you?

This syntax will run only on 5 minutes past the hour. To run every 5 minutes, you want:

*/5 * * * * …

Hi,
it seems to be working now, command below does the trick.

*/5 * * * * /home/joejoe/web/django-site/virtual/bin/python /home/joejoe/web/django-site/manage.py runcrons > /home/joejoe/cronjob.log 2>&1

@Josylad --

You should read this:

https://help.dreamhost.com/hc/en-us/articles/215767107-Execution-environment-of-a-cron-job

The money quote is the first paragraph:

The cron daemon was designed in such a way that it does NOT execute commands within your normal shell environment. This means you cannot use bare commands in cron the way you would from the SSH shell command line. This is because the PATH environment variable is /usr/bin:/bin, and the SHELL environment variable is set to /bin/sh.

In short, the cron daemon is not a shell…and doesn't play one on TV.

-- sw

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct