Prioritizing System Services with Systemd - Run a Process Permanently in the Background

If you simply want to adjust some priorities, here is how to do it in three steps.
  1. sudo systemctl edit [service]
  2. sudo systemctl daemon-reload
  3. sudo systemctl restart [service]
Insert this into the edit prompt e.g. for a background process with low priority, that tends to interfere with other disk accesses:

[Service]
CPUShares=100 # default 1024
MemoryLimit=500M #default no limit
BlockIOWeight=50 # default 1000/500.
Nice=19 
IOSchedulingClass=idle
CPUSchedulingPolicy=idle

The higher the values, the more you permit the service. You can no longer add manual cgroups like this in recent systemd:

ControlGroupAttribute=memory.swappiness 70

You can check the cgroups assignment with systemd-cgtop and this script, run as e.g. check_cgroups smbd:

#!/bin/bash
PID=$(ps -eo pid,comm | grep $1 | awk '{print $1}')
set -x
for i in $PID; do
cat /proc/$i/cgroup
done
cat /sys/fs/cgroup/blkio/system.slice/$1.service/blkio.weight
cat /sys/fs/cgroup/cpu/system.slice/$1.service/cpu.shares
cat /sys/fs/cgroup/memory/system.slice/$1.service/memory.limit_in_bytes 


References:

  • A thorough (slightly outdated) introduction to cgroups in systemd is part of the Administrator's Manual.
  • An overview of all options systemd exec accepts inside a service file for how to run a process.

No comments:

Post a Comment

I appreciate comments. Feel free to write anything you wish. Selected comments and questions will be published.