Starting Kolibri as a Service on Linux (systemd)

Hi,

Starting Kolibri automatically and easily maintaining it as a service is important for convenience and security.

So I created a user kolibri with home directory /opt/kolibri. The following is used to test running Kolibri:

sudo -u kolibri /bin/bash -c “cd /opt/kolibri; /usr/bin/python /opt/kolibri/bin/kolibri-v0.7.0.pex start”

It worked fine.

Next I try starting with a systemd service as follows:

$ cat /usr/lib/systemd/system/kolibri-pex.service
[Unit]
Description=Kolibri (pex) server
[Service]
Type=simple
PIDFile=/run/kolibri-pex.pid
#WorkingDirectory=/opt/kolibri
User=kolibri
SyslogIdentifier=kolibri-pex
ExecStart=/usr/bin/python /opt/kolibri/bin/kolibri-v0.7.0.pex start
ExecStop=/usr/bin/python /opt/kolibri/bin/kolibri-v0.7.0.pex stop
[Install]
WantedBy=multi-user.target

Normally type should be forking instead of simple since Kolibri goes into daemon mode by default. However if I put forking then starting the service just hangs.

Followed by

sudo systemctl start kolibri-pex
sudo systemctl status kolibri-pex
● kolibri-pex.service - Kolibri (pex) server
Loaded: loaded (/usr/lib/systemd/system/kolibri-pex.service; disabled; vendor preset: disabled)
Active: inactive (dead)

Jan 27 13:44:57 linux.local kolibri-pex[22135]: http://127.0.0.1:8080/
Jan 27 13:44:57 linux.local kolibri-pex[22135]: INFO Going to daemon mode, logging to /opt/kolibri/.kolibri/server.log
Jan 27 13:44:57 linux.local kolibri-pex[22135]: 0 static files copied to ‘/opt/kolibri/.kolibri/static’, 406 unmodified.
Jan 27 13:44:57 linux.local kolibri-pex[22135]: Operations to perform:
Jan 27 13:44:57 linux.local kolibri-pex[22135]: Apply all migrations: contenttypes, logger, kolibriauth, admin, exams, morango, content, device, auth, sessions
Jan 27 13:44:57 linux.local kolibri-pex[22135]: Running migrations:
Jan 27 13:44:57 linux.local kolibri-pex[22135]: No migrations to apply.
Jan 27 13:44:57 linux.local kolibri-pex[22135]: Your models have changes that are not yet reflected in a migration, and so won’t be applied.
Jan 27 13:44:57 linux.local kolibri-pex[22135]: Run ‘manage.py makemigrations’ to make new migrations, and then re-run ‘manage.py migrate’ to apply them.
Jan 27 13:44:59 linux.local kolibri-pex[22147]: INFO Running Kolibri with the following settings: kolibri.deployment.default.settings.base

However in the browser http://127.0.0.1:8080 I get site cannot be reached, even if I uncomment > WorkingDirectory=/opt/kolibri

Kolibri’s log shows:

INFO 2018-01-27 13:53:57,261 apps Running Kolibri with the following settings: kolibri.deployment.default.settings.base
INFO 2018-01-27 13:53:57,391 cli Running update routines for new version…
INFO 2018-01-27 13:53:57,642 cli Running ‘kolibri start’ as daemon (system service)
INFO 2018-01-27 13:53:57,651 cli Kolibri running on:

INFO 2018-01-27 13:53:57,651 cli Going to daemon mode, logging to /opt/kolibri/.kolibri/server.log
INFO 2018-01-27 13:53:57,806 ping Attempting a ping.
INFO 2018-01-27 13:53:57,812 utils Started new MESSAGEPROCESSOR thread ID#oscar-cold-hot-artist
INFO 2018-01-27 13:53:57,813 utils Started new SCHEDULER thread ID#cup-illinois-mirror-nuts
INFO 2018-01-27 13:53:57,815 utils Started new WORKERMESSAGEHANDLER thread ID#grey-wisconsin-nitrogen-bulldog
INFO 2018-01-27 13:53:57,874 ping data: {“instance_id”: “f7242de04a60551bc038c8ab4abe8369”, “version”: “0.7.0”, “mode”: “”, “platform”: “Linux-4.14.14-1-MANJARO-x86_64-with-arch-Manjaro-Linux”, “sysversion”: “3.6.4 (default, Dec 23 2017, 19:07:07) \n[GCC 7.2.1 20171128]”, “database_id”: “9eae25984d974535b72540fd5d6bbbe1”, “system_id”: “”, “node_id”: “fd2d9eb6c940191f3a28”, “language”: “”, “uptime”: 0}
INFO 2018-01-27 13:53:59,216 apps Running Kolibri with the following settings: kolibri.deployment.default.settings.base

Do you have a ready made kolibri systemd service definition? If not, how to debug this case?

Best wishes

Further discoveries:

sudo systemctl start kolibri

The above does start kolibri, the browser shows this OK. However the command just hangs, as already mentioned. If I do a CTRL-C to interrupt it followed by status, I get this

sudo systemctl status kolibri
● kolibri.service - Kolibri server
Loaded: loaded (/usr/lib/systemd/system/kolibri.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/kolibri.service.d
└─override.conf
Active: activating (start) since Sun 2018-01-28 10:59:45 WITA; 16s ago
Process: 20778 ExecStart=/usr/bin/kolibri start (code=exited, status=0/SUCCESS)
Tasks: 37 (limit: 4915)
CGroup: /system.slice/kolibri.service
└─20785 /usr/bin/python /usr/bin/kolibri start

Jan 28 10:59:46 linux.local kolibri[20778]: http://127.0.0.1:8087/
Jan 28 10:59:46 linux.local kolibri[20778]: INFO Going to daemon mode, logging to /opt/kolibri/server.log
Jan 28 10:59:46 linux.local kolibri[20778]: 0 static files copied to ‘/opt/kolibri/static’, 519 unmodified.
Jan 28 10:59:46 linux.local kolibri[20778]: Operations to perform:
Jan 28 10:59:46 linux.local kolibri[20778]: Apply all migrations: kolibriauth, logger, auth, exams, device, admin, contenttypes, morango, sessions, content
Jan 28 10:59:46 linux.local kolibri[20778]: Running migrations:
Jan 28 10:59:46 linux.local kolibri[20778]: No migrations to apply.
Jan 28 10:59:46 linux.local kolibri[20778]: Your models have changes that are not yet reflected in a migration, and so won’t be applied.
Jan 28 10:59:46 linux.local kolibri[20778]: Run ‘manage.py makemigrations’ to make new migrations, and then re-run ‘manage.py migrate’ to apply them.
Jan 28 10:59:46 linux.local systemd[1]: kolibri.service: PID file /run/kolibri.pid not readable (yet?) after start: No such file or directory

The service running OK.
If the service is enabled to start at boot, then Kolibri does starts fine (Type=forked).
systemctl stop Kolibri also works.

However after a certain time the service times out:
Jan 28 11:23:49 linux.local systemd[1]: kolibri.service: Start operation timed out. Terminating.
Jan 28 11:23:49 linux.local systemd[1]: kolibri.service: Failed with result ‘timeout’.
Jan 28 11:23:49 linux.local systemd[1]: Failed to start Kolibri server.

After which it is not accessible in the browser.

**Conclusion Kolibri is not going into daemon mode when run under systemd.

This is what we use.

[Unit]
Description=Kolibri

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/kolibri start
ExecStop=/usr/local/bin/kolibri stop
User=kolibri

[Install]
WantedBy=multi-user.target

cyberorg, that did it!
Much obliged.