[systemd-devel] Start a global service

Damien Robert damien.olivier.robert+gmane at gmail.com
Wed Feb 15 16:58:02 UTC 2017


1) Feature request

I would like a way [as root] to start a service on all active user sessions.
Typically to start a user service that was installed globally.

sudo systemctl --global start myservice.service

will start myservice.service as a root user service, not on the other user
sessions.

2) Feature request

Something that could be usefull too is a special target (both for the user
and system sessions) that is started automatically when there is ac loss,
so that we run on battery, and conversely. We could even imagine systemd
calling hooks like for systemd-sleep.

It is of course not hard to do that on the system session with a udev rule,
but I don't know how to start one automatically on the user sessions (hence
my feature request above).

I call my targets power-save.target and power-performance.target, but the
name may not be generic enough, maybe something like ac-loss.target,
ac-gain.target?

3) Usage

Here is my current implementation of a system wide special power-save target.

- In /etc/udev/rules.d, a rule:
ACTION=="change", SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="[01]", RUN+="/usr/local/lib/powersave/bin/power-dispatcher %E{POWER_SUPPLY_ONLINE}"

- power-dispatcher:
if (($1)); then # 1 = plug
  systemctl --no-block start power-performance.target
else # 0 = battery
  systemctl --no-block start power-save.target
fi

- power-performance.target:
[Unit]
Description=System power management: performance mode
Conflicts=power-save.target

- power-save.target:
[Unit]
Description=System power management: power-saving mode
Conflicts=power-performance.target
After=power-performance.target

- power-save.service:
[Unit]
Description=Powersave mode (global service)
PartOf=power-save.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStartPre=-/usr/bin/sleep 10
ExecStart=/usr/local/lib/powersave/bin/powersave true

[Install]
WantedBy=power-save.target

- power-performance.service:
[Unit]
Description=Power performance mode (global service)
PartOf=power-performance.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/lib/powersave/bin/powersave false

[Install]
WantedBy=power-performance.target

Note: the sleep 10 in power-save.service is that in case of <10s ac loss, like happen often in trains or planes the powersave settings do not get applied.

And it works very well!

However for X display settings like 'xset +dpms', it would make more sense
to run this as part of the user session, which has access to DISPLAY and
XAUTHORITY. It is not too hard to find the active display as roots, but the
DISPLAY without the XAUTHORITY is not enough (unless the user adds a 'xhost
+si:localuser:$(id -un) somewhere'). So I could try to find which user is
using which X display and find their .Xauthority but it gets cumbersome.

Thanks,
Damien Robert



More information about the systemd-devel mailing list