Shahrukh Mohammad
Shahrukh Mohammad

Reputation: 1105

Running Django Management command in crontab

I am trying to run the management command python manage.py process_tasks provided by Django background tasks to run my background tasks. I want to run them as a cronjob.

To do this I am creating a entry in cron tab using the command sudo crontab -e

My crontab entry looks something like this -

*/1 * * * * . /var/www/cronjob.sh >> /var/www/crontab.log 2>&1

and the contents of the shell script which I am running here are -

#!/bin/bash
while true
do
 echo 'starting'
 sudo su ubuntu
 . /var/www/myproject/env/bin/activate
 . /var/www/myproject/.shahrukh_aliases
 python /var/www/myproject/src/manage.py process_tasks
 echo 'finished'
 sleep 2
done

But the problem is that my EC2 instance crashes after some time and runs only after I restart it. I get the following error

-bash: fork: Cannot allocate memory

I think it's consuming all the memory and hence my instance crashes. I don't know how to run the task in cron job and why it is consuming my memory. I want to know how can I run this job as a cronjob. Thanks for the help in advance

Upvotes: 1

Views: 1578

Answers (1)

Shahrukh Mohammad
Shahrukh Mohammad

Reputation: 1105

The solution to my problem was simple. I can still do it using crontab. I just had to provide an extra parameter to the process_tasks command.

So my command which I have to run from crontab is

python manage.py process_tasks --duration 59

since I am running the job after every minute and the above command will run background tasks for 59 seconds and then end itself which will then be restarted by cron.

Upvotes: 1

Related Questions