systems-guide/systemd.md
2025-11-18 14:44:53 +01:00

99 lines
2.4 KiB
Markdown

# Configuring, running and logging with `systemd`
## Why you want this
`systemd` provides an out-of-the box linux native way of running and logging for your services.
You could as well use things like `tmux`, `screen`, `pm2` or just a background task, but they dont cut all the cases.
`systemd` provides a way to declaratively define your app startup, re-startup, service dependencies and pipe its outputs in a structured manner to `journalctl` where they are stored in the most efficient manner.
## How to create a new service that runs persistently even if server restarts
First create your new service file
```sh
sudo nvim /etc/systemd/system/$SERVICE_NAME.service
```
then paste and edit this template:
```toml
[Unit]
Description=Service name
After=network.target
Wants=network.target
[Service]
# Set the working directory where your binary resides
WorkingDirectory=/srv/YOUR_PATH
# Absolute path to your binary
ExecStart=/srv/YOUR_PATH/target/release/BINARY_NAME
# Ensure the service restarts on failure
Restart=always
RestartSec=5
# Optional: prevent rapid restart loops
StartLimitIntervalSec=60
StartLimitBurst=3
# Direct stdout and stderr to the system journal
StandardOutput=journal
StandardError=journal
SyslogIdentifier=YOUR_SERVICE
# Optional: increase file descriptor limits if needed
LimitNOFILE=4096
[Install]
WantedBy=multi-user.target
```
then enable and start your service
```sh
sudo systemctl enable $YOUR_SERVICE
sudo systemctl start $YOUR_SERVICE
```
it will now keep running even after restarts.
## Logging your server
A better guide: [click here](https://www.loggly.com/ultimate-guide/using-journalctl/)
#### To get all logs simply do
```sh
journalctl -u your_service
```
#### To follow logs append `-f` option.
```sh
journalctl -u your_service -f
```
#### To query logs by time use `--since` and `--until`
```sh
# it uses a human readable way. You can operate with hours, seconds etc.
journalctl -u your_service --since "2 hours ago"
# no timestamps, you need the below format to do precise times
journalctl -u your_service --since "2015-06-26 23:15:00" --until "2015-06-26 23:20:00"
```
#### To get precise number of logs, use `-n` option.
```sh
journalctl -u your_service -n 100
```
If you want to get latest N messages, use also the `-r` option to reverse the log order to get latest messages.
```sh
# Get last 100 logs
journalctl -u your_service -r -n 100
```