Can any virtual environment start systemd gunicorn.service for all virtuenenv?

CodeMan asked:

If you are not working inside an environment, Gunicorn will be installed globally (i.e. available systemwide). This is not recommended. Always opt for using virtualenv. But if you have multiple virtualenvs how many gunicorn.service files should you have to start gunicorn on system startup? For example below

WorkingDirectory=/home/myproject...
ExecStart==/home/myproject/myprojectenv/bin/gunicorn...

Can any virtual environment start it for all virtuenenv?

My answer:


You can use systemd instances for this.

For instance, let’s say you create all your virtualenvs in the same directory (this will make it easier anyway).

Then you can write a unit file like this:

WorkingDirectory=/home/myproject/%i
ExecStart=/home/myproject/%i/bin/gunicorn ...

The %i represents a particular instance of the unit, of which there can be no limit. You refer to an instance by using an @ sign followed by the instance name, such as:

systemctl enable gunicorn@myprojectenv.service
systemctl start gunicorn@myprojectenv.service

Thus %i will be replaced with myprojectenv. Repeat this for every instance you want to create.

Further reading:


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.