Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a shell command inside/inline of a systemd service file

I am running the gunicorn server as a service via systemd, Here is the sample service file:

[Unit]
Description=Gunicorn NGINX
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/test
ExecStart=/usr/local/bin/gunicorn --workers 8 --threads 8 --backlog 100 --bind 10.0.0.20:5000 -m 777 abc:app
Restart=always

[Install]
WantedBy=multi-user.target

I want now to replace the number near --workers and --threads by number of cores using the shell command so that it will dynamically pick the number of cores

nproc --all

Can someone help me how to do this

like image 398
Venkata S S K M Chaitanya Avatar asked Jan 13 '18 03:01

Venkata S S K M Chaitanya


People also ask

What is ExecStop systemd?

The ExecStop setting is optional and is used to communicate with the service for a clean termination. The process specified by ExecStop will run in case the service crashes.

What is Type forking in systemd?

Type=forking : systemd considers the service started up once the process forks and the parent has exited. For classic daemons use this type unless you know that it is not necessary. You should specify PIDFile= as well so systemd can keep track of the main process.


1 Answers

You can explicitly invoke a shell to get shell parsing.

ExecStart=/bin/bash -c '/usr/local/bin/gunicorn --workers "$(nproc --all)" --threads "$(nproc --all)" --backlog 100 --bind 10.0.0.20:5000 -m 777 abc:app'
like image 164
John Kugelman Avatar answered Nov 15 '22 07:11

John Kugelman