[][src]Constant blogs::_2018_::october::_15_

pub const _15_: ()

Running a Rust web server with systemd

I have one note ahead: I'm an amateur programmer. So my experiences should only be used as references.

Why systemd?

Quoting from its website:

systemd is a suite of basic building blocks for a Linux system. It provides a system and service manager that runs as PID 1 and starts the rest of the system. systemd provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux control groups, maintains mount and automount points, and implements an elaborate transactional dependency-based service control logic. systemd supports SysV and LSB init scripts and works as a replacement for sysvinit. Other parts include a logging daemon, utilities to control basic system configuration like the hostname, date, locale, maintain a list of logged-in users and running containers and virtual machines, system accounts, runtime directories and settings, and daemons to manage simple network configuration, network time synchronization, log forwarding, and name resolution.

From my view, it's basically a process manager.

Quick use

Normally, a service file is put in /etc/systemd/system/ with .service extension. For example:

[Unit]
Description=API-1

[Service]
ExecStart=/home/api1/bin/api1
WorkingDirectory=/home/api1/bin/
LimitCPU=infinity
LimitCORE=infinity
LimitNOFILE=infinity
MemoryLimit=infinity

[Install]
WantedBy=multi-user.target

Note that some default settings are very limited. For example I called libc::getrlimit() of libc::RLIMIT_NOFILE under root user, and got 1_048_576. But then calling that when running as a service, I got 4096 (or 4095, I don't remember correctly). That's why I added some limit flags to the sample.

Enabling/disabling a service

To enable:

sudo systemctl enable --now file-name.service

To disable, replace enable with disable.

Viewing logs

Human readable:

systemctl status file-name.service

Parsable by machine:

systemctl show file-name.service

Conclusion

That's it :-D

I guess you might want to try with your test projects first.