Foreman process runner with systemd

Suppose we have project named stp (some test project). For example it will have 2 processes, web application (python Django started with gunicorn) and python celery worker

Install foreman:

gem install foreman

Create Procfile in project dir:

web: /envs/stp/bin/gunicorn --reload stp.wsgi -w 3
celery: /envs/stp/bin/celery -A stp.celery_app worker

To test process working:

foreman start web

Lets export to systemd .service file

sudo foreman export -p5000 --app stp --user username_to_start_from systemd /etc/systemd/system/

The output will be like this:

[foreman export] cleaning up directory: /etc/systemd/system//graphical.target.wants
[foreman export] writing: [email protected]
[foreman export] creating: stp-web.target.wants
[foreman export] symlinking: stp-web.target.wants/[email protected] -> ../[email protected]
[foreman export] writing: stp-web.target
[foreman export] writing: [email protected]
[foreman export] creating: stp-celery.target.wants
[foreman export] symlinking: stp-celery.target.wants/[email protected] -> ../[email protected]
[foreman export] writing: stp-celery.target
[foreman export] writing: stp.target

see about systemd units here: https://maketips.net/tip/103/systemd-units-basics

Instance name here means PORT environment variable, you can ensure about this seen:

 cat /etc/systemd/system/[email protected] | head -n 10
[Unit]
PartOf=stp-web.target

[Service]
User=username_to_start_from 
WorkingDirectory=/srv/stp/
Environment=PORT=%i
... HERE OTHER DIRICTIVES FROM YOUR .env file ...
Environment=DEBUG=0
...

So if you use gunicorn it will use that port to listen on.

Examples of ussage

Start only gunicorn on port 5700:

systemctl start stp-[email protected]5700.service

Start whole target:

systemctl start stp.target

Enable whole target (start on OS boot):

systemctl enable stp.target

Note that in this case it will start on default (5000) port. To change it

change symlink name, and reload daemon:

mv stp-web.target.wants/stp-web@5000.service stp-web.target.wants/stp-web@PORT_YOU_WANT.service
sudo systemctl daemon-reload
sudo systemctl restart stp.target

You can also simply define port when call foreman export -pXXXX, but it should be multiple of 1000, e.g. 1000, 2000, 3000, 45000 etc.

also

Analogical upstart case (for systems with init scripts instead of systemd):

sudo foreman export --app web --us

#systemd
0
Ivan Borshchov profile picture
Dec 27, 2016
by Ivan Borshchov
Did it help you?
Yes !
No

Best related