Sometimes you want your machine to save energy, sometimes you need it to stay awake until a long-running job finishes. Here’s how I balanced both on my Debian desktop.
Christian Domingues Pexels
So I now find myself wanting to understand how systemd
works on Debian. (There is web information here:](https://wiki.debian.org/systemd) ) Basically, my desktop has an energy saver built in, and after a few minutes of idle time it goes to sleep. I thoroughly approve of this. However, I have a few scripts that take a long time to run. One is whenever I’m fitting models with McMC (I have a plan to run these on a server which solves that problem in a different way). However, one I can’t avoid is database imports, and some queries (I have a few slow queries that run as materialized views for example).
My first foray into this was looking at systemd-inhibit --list
, which shows me (for example) that NetworkManager can interupt a sleep while it shuts down some connections. An example of the output is given below:
WHO UID USER PID COMM WHAT WHY MODE
make 1000 me 4321 make sleep Running importer scripts block
So, I’ve been using sqitch
for database migrations, I had a messy assembly of bash scripts I’d been using to import data into PostgreSQL. I’ve been trying to tidy that using make
(which I will write about separately). My makefile now contains the line:
# Use systemd-inhibit to block suspend while scripts are running
INHIBIT = systemd-inhibit --what=sleep --why="Running importer scripts"
# If we're not already inside systemd-inhibit, wrap the top-level call
# so the machine won't suspend until imports are done
ifndef NO_INHIBIT
ifeq ($(shell systemd-inhibit --list | grep -q "Running importer scripts" && echo yes || echo no),no)
# Wrap make recursively with inhibit
MAKE := $(INHIBIT) $(MAKE) NO_INHIBIT=1
endif
endif
What this means is that make
invokes
systemd-inhibit --what=sleep --why="Running importer scripts" make NO_INHIBIT=1
on first calling, but all the recursive calls have On recursive invocations NO_INHIBIT=1
ensures make
doesn’t wrap itself again (this hopefully avoid infinite loops).
This means my computer should stay awake until all the importer scripts have been run through to completion, but the energy saving feature is restored after the scripts have completed. More information is available from man systemd-inhibit
Use the share button below if you liked it.
It makes me smile, when I see it.