11 daemon
tony (AK) edited this page 2021-09-09 01:52:35 -07:00

systemd

in this example you have created a user named matrix-registration and you have installed matrix-registration to a virtual environment in a folder called /opt/matrix-registration

###[Unit]

[Unit]
Description=Matrix-registration daemon
After=matrix-synapse.service
BindsTo=matrix-synapse.service

[Service]
Type=simple
User=matrix-registration
WorkingDirectory=/opt/matrix-registration
ExecStart=/opt/matrix-registration/bin/python bin/matrix-registration serve
Restart=always

[Install]
WantedBy=multi-user.target

(provided by @olmari:hacklab.fi)

Now just enable and start the unit with:

$ systemctl enable matrix-registration
$ systemctl start matrix-registration

Alternative: systemd/virtual environment

in this example you have created a virtual environment named matrix-registration using venv and you have installed matrix-registration to a virtual environment in a folder called /opt/matrix-registration. in this virtual environment case we are only adding Environment and Group systemd options so process runs as respective matrix-registration user/group

[Unit]
Description=Matrix-registration daemon
After=matrix-synapse.service
BindsTo=matrix-synapse.service

[Service]
Type=simple
User=matrix-registration
Group=matrix-registration
WorkingDirectory=/opt/matrix-registration
Environment=PYTHONPATH=/opt/matrix-registration/bin:$PATH
ExecStart=/opt/matrix-registration/bin/python bin/matrix-registration --config-path config/config.yaml serve
Restart=on-failure

[Install]
WantedBy=multi-user.target

(provided by @ak:kode.im)

Now just enable and start the unit with:

$ systemctl enable matrix-registration
$ systemctl start matrix-registration

Alternative: systemd/user

a very easy way to have the app running in the background and start on reboot is a systemd/user unit. in this example your username is matrix and you have your config.yaml in a folder called ~/matrix-registration/.

just create a file ~/.config/systemd/user/matrix-registration.service with:

[Unit]
Description=Matrix Registration

[Service]
WorkingDirectory=/home/matrix/matrix-registration/
Type=simple
ExecStart=matrix-registration serve
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

Now just enable and start the unit with:

systemctl --user enable matrix-registration
systemctl --user start matrix-registration

To make sure the systemd file starts right after boot execute this:

# loginctl enable-linger username

See https://wiki.archlinux.org/index.php/Systemd/User#Automatic_start-up_of_systemd_user_instances for more information

FreeBSD rc.d

In this example pip installed the binary at /usr/local/bin/matrix-registration:

Create user: pw adduser matrixreg -d /nonexistent -s /usr/sbin/nologin -c "matrix-registration user"

Create working directory & set permissions: mkdir /var/db/matrixreg && chown -R matrixreg:matrixreg /var/db/matrixreg

Create config: cd /usr/local/etc && fetch https://raw.githubusercontent.com/ZerataX/matrix-registration/master/config.sample.yaml && mv config.sample.yaml matrix-registration.yaml && nano matrix-registration.yaml:

...
server_location: 'http://192.168.1.78:8008'
server_name: 'example.tld'
shared_secret: 'Registration_Shared_Secret'
admin_secret: 'APIAdminPassword'
...
db: 'sqlite:////var/db/matrixreg/db.sqlite3'
...
logging:
 handlers:
   file:
     filename: /var/db/matrixreg/m_reg.log

Create rc.d startup script nano /usr/local/etc/rc.d/matrixreg:

#!/bin/sh
#
# PROVIDE: matrixreg
# REQUIRE:
# KEYWORD:

. /etc/rc.subr

name="matrixreg"
rcvar="${name}_enable"
matrixreg_command="/usr/local/bin/matrix-registration --config-path=/usr/local/etc/matrix-registration.yaml serve"
matrixreg_interpreter="/usr/local/bin/python3.7"
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-P ${pidfile} -u matrixreg -r -f ${matrixreg_command}"

load_rc_config $name

run_rc_command "$1"

Make startup script executable: chmod +x /usr/local/etc/rc.d/matrixreg

Enable script on startup: sysrc matrixreg_enable="YES"

Start the service: service matrixreg start