summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-01-25 00:56:34 +0100
committerAki <please@ignore.pl>2024-01-25 00:56:34 +0100
commit4a0697a0e19fcf7b19abbabffb14cba33aee3491 (patch)
tree1d16f245d2098ac4f08aa42b5b546cdcd5569e86
downloadsystemd-ready-for-shutdown-4a0697a0e19fcf7b19abbabffb14cba33aee3491.zip
systemd-ready-for-shutdown-4a0697a0e19fcf7b19abbabffb14cba33aee3491.tar.gz
systemd-ready-for-shutdown-4a0697a0e19fcf7b19abbabffb14cba33aee3491.tar.bz2
Naive approach that seems to work with mayohiga
One of the important flaws is that if one of the intended services will not run, then shutdown may not happen at all. With current mayohiga setup this will not happen but it may break in any other case.
-rw-r--r--.gitignore2
-rw-r--r--Makefile39
-rw-r--r--ready-for-shutdown.target5
-rw-r--r--ready-for-shutdown.target.8.in18
-rwxr-xr-xshutdown-if-no-sessions4
-rw-r--r--shutdown-if-no-sessions.service.81
-rw-r--r--shutdown-if-no-sessions.service.in7
7 files changed, 76 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..92389d9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.service
+*.target.8
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..9984122
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,39 @@
+DESTDIR=
+PREFIX=/usr/local
+LIBEXECDIR=$(PREFIX)/libexec
+LIBDIR=$(PREFIX)/lib
+SYSDDIR=$(LIBDIR)/systemd/system
+DATADIR=$(PREFIX)/share
+MANDIR=$(DATADIR)/man
+MAN8DIR=$(MANDIR)/man8
+
+
+%: %.in
+ sed "s|@LIBEXECDIR@|$(LIBEXECDIR)|g" $< >$@
+ sed -i "s|@SYSDDIR@|$(SYSDDIR)|g" $@
+
+
+all: shutdown-if-no-sessions.service ready-for-shutdown.target.8
+
+
+install: all
+ install -m644 -Dt $(DESTDIR)$(SYSDDIR) ready-for-shutdown.target
+ install -m644 -Dt $(DESTDIR)$(MAN8DIR) ready-for-shutdown.target.8
+ install -m644 -Dt $(DESTDIR)$(SYSDDIR) shutdown-if-no-sessions.service
+ install -m644 -Dt $(DESTDIR)$(MAN8DIR) shutdown-if-no-sessions.service.8
+ install -m755 -Dt $(DESTDIR)$(LIBEXECDIR) shutdown-if-no-sessions
+
+
+uninstall:
+ rm -f $(DESTDIR)$(SYSDDIR)/ready-for-shutdown.target
+ rm -f $(DESTDIR)$(MAN8DIR)/ready-for-shutdown.target.8
+ rm -f $(DESTDIR)$(SYSDDIR)/shutdown-if-no-sessions.service
+ rm -f $(DESTDIR)$(MAN8DIR)/shutdown-if-no-sessions.service.8
+ rm -f $(DESTDIR)$(BINDIR)/shutdown-if-no-sessions
+
+
+clean:
+ rm -f *.service *.target.8
+
+
+.PHONY: all install uninstall clean
diff --git a/ready-for-shutdown.target b/ready-for-shutdown.target
new file mode 100644
index 0000000..10e8191
--- /dev/null
+++ b/ready-for-shutdown.target
@@ -0,0 +1,5 @@
+[Unit]
+Description=All scheduled timers and builds are done
+Documentation=man:ready-for-shutdown.target(8)
+Before=shutdown-if-no-sessions.service
+Wants=shutdown-if-no-sessions.service
diff --git a/ready-for-shutdown.target.8.in b/ready-for-shutdown.target.8.in
new file mode 100644
index 0000000..babba56
--- /dev/null
+++ b/ready-for-shutdown.target.8.in
@@ -0,0 +1,18 @@
+.TH ready-for-shutdown.target 8 "2024-01-25"
+.SH NAME
+ready-for-shutdown.target, shutdown-if-no-sessions.service - systemd target that marks completion of routine boot tasks
+.SH SYNOPSIS
+.B ready-for-shutdown.target
+.br
+.B shutdown-if-no-sessions.service
+.SH DESCRIPTION
+Target is intended to mark the completion of all scheduled tasks and readiness for shutdown that is triggered
+immediately after. Before enabling this target add
+.B [Install]
+section listing all units that are intended to be run before shutting down.
+.SH FILES
+.I @SYSDDIR@/ready-for-shutdown.target
+.br
+.I @SYSDDIR@/shutdown-if-no-sessions.service
+.br
+.I @LIBEXECDIR@/shutdown-if-no-sessions
diff --git a/shutdown-if-no-sessions b/shutdown-if-no-sessions
new file mode 100755
index 0000000..285eb70
--- /dev/null
+++ b/shutdown-if-no-sessions
@@ -0,0 +1,4 @@
+#!/bin/sh
+if ! who | grep -e tty -e pts; then
+ systemctl poweroff
+fi
diff --git a/shutdown-if-no-sessions.service.8 b/shutdown-if-no-sessions.service.8
new file mode 100644
index 0000000..54b70c1
--- /dev/null
+++ b/shutdown-if-no-sessions.service.8
@@ -0,0 +1 @@
+.so ready-for-shutdown.target.8
diff --git a/shutdown-if-no-sessions.service.in b/shutdown-if-no-sessions.service.in
new file mode 100644
index 0000000..f8d6748
--- /dev/null
+++ b/shutdown-if-no-sessions.service.in
@@ -0,0 +1,7 @@
+[Unit]
+Description=Shutdown if nobody is logged in
+Documentation=man:shutdown-if-no-sessions.service(8)
+
+[Service]
+Type=oneshot
+ExecStart=@LIBEXECDIR@/shutdown-if-no-sessions