Search code examples
djangoubuntucelerysystemdupstart

Ubuntu service Upstart or SystemD, Django development server as Service


I've been working around with Python & Django Framework for a while with Ubuntu 16.01. Since I used Django with Q system (Celery) and some others Enhancement Apps. When I try to run all the apps each time, I need to run development server "{python manage.py runserver}", then running Celery Worker "{celery -A filename worker -l info}". Each time I working, it takes me minutes to enter the directory and start it up. I surf around and come up with the idea of setup it as service. Example, service name: "pyd". I just need to run "{sudo pyd start}" -> then Django Development Server and Celery will start, and if I run "{sudo pyd stop}" -> then Django & Celery will stop.

I try to search around, and things start to confuse me between "Upstart" and "Systemd".

Could any one suggest, me how to make both Django and Celery as Service run in Ubuntu ? between "Upstart" & "Systemd" which one is better ??

Source code to indicate sample is appreciated.

Thank


Solution

  • You can use Upstart to do this.

    Post installation, go to the directory /etc/init/. Create a file xyz.conf and add the lines:

    cd /path/to/your/manage.py/file
    exec python manage.py runserver & celery -A filename worker -l info
    

    If you are using a virtualenv, add the following lines before the above:

    pre-start script
        #activate virtual environment
        source env-name/bin/activate
    end script
    

    Now, you can start Django Dev Server and Celery as a service by issuing the commands, sudo start xyz, stop it by issuing sudo stop xyz and check status of your service by issuing sudo status xyz.

    xyz.conf will log into /var/log/upstart/xyz.log. You can view logs with the following command: sudo tail -f /var/log/upstart/xyz.log.