Ubuntu service Upstart or SystemD, Django development server as Service

删除回忆录丶 提交于 2019-12-13 03:46:33

问题


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


回答1:


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.




回答2:


You can setup celery as daemon service, follow the below steps

Step 1: celery init script

Copy this script file in the directory /etc/init.d/celeryd (celeryd will be the service name, you can name it anything)

The instruction to set this up is given in the above source.

Step 2: Setting up celery configuration

Now configure celery configuration in this file /etc/default/celeryd, this example will give you a generic configuration file.

Now you can run celery as service with following commands

sudo service celeryd start

sudo service celeryd stop

or any other service commands.



来源:https://stackoverflow.com/questions/46904319/ubuntu-service-upstart-or-systemd-django-development-server-as-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!