Ubuntu service Upstart or SystemD, Django developm

2019-08-25 02:43发布

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

2条回答
We Are One
2楼-- · 2019-08-25 03:02

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-25 03:20

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.

查看更多
登录 后发表回答