You are not logged in.

#1 2016-04-24 10:59:33

jobach
Member
Registered: 2015-09-28
Posts: 5

[SOLVED] Run systemd service when AC power is connected

I have a service (say foo.service), of type simple (or forking, for that matter).

I wan't this service to be started every time when my notebook is connected to the power supply, and to be stopped again once it goes back to battery mode.

I know of ConditionACPower=, but this only checks once on the startup of the unit if the power is connected, and does not help triggering the unit in the first place.

Is there a way to achieve this? If not using systemd, then at least in some other way (e.g. calling a specific executable on AC Power events)?

Last edited by jobach (2016-04-24 23:50:47)

Offline

#2 2016-04-24 18:33:03

frank604
Member
From: BC, Canada
Registered: 2011-04-20
Posts: 1,212

Re: [SOLVED] Run systemd service when AC power is connected

This sounds like an xy problem. How about you detail what you are trying to accomplish and why? Perhaps we can set you in a better direction?

Offline

#3 2016-04-24 18:41:25

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: [SOLVED] Run systemd service when AC power is connected


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#4 2016-04-24 23:05:05

jobach
Member
Registered: 2015-09-28
Posts: 5

Re: [SOLVED] Run systemd service when AC power is connected

To specify my problem:

I'm using the i3 window manager, and am expirencing some problems with tearing (see https://wiki.archlinux.org/index.php/I3#Tearing). As the wiki page suggests, Compton helps with that. However, compton increases my CPU load (not much admittedly, but it bugged me anyway). So I only wan't it to run when the power supply is connected.

My solution (using acpid)

I now have acpid start a systemd target I created, like this:

/etc/acpi/handler.sh
----------------------------
#!/bin/bash
[...]
case "$4" in
    00000000)
        logger 'AC unpluged'
        systemctl stop ac-power.target
        ;;
    00000001)
        logger 'AC pluged'
        systemctl start ac-power.target
        ;;
    *)
        logger "Don't know what to do: $4"
        ;;
esac
[...]

The target looks like this:

/etc/systemd/system/ac-power.target
---------------------------------------------------
[Unit]
Description=AC-only services

And the compton-services:

/etc/systemd/system/compton.service
[Unit]
Description=Compton - a compositor for X11
PartOf=ac-power.target

[Service]
Environment="DISPLAY=:0"
User=jobach
ExecStart=/usr/bin/compton [...]

[Install]
WantedBy=ac-power.target

The PartOf= is needed, so the service get's stopped once the targret is stopped (StopWhenUnneeded= might also work).

Last edited by jobach (2016-04-24 23:07:33)

Offline

#5 2016-04-24 23:40:07

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: [SOLVED] Run systemd service when AC power is connected

So, is it solved?  If so, please prepend [SOLVED] to your thread title.  Thanks.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#6 2016-04-25 20:34:14

frank604
Member
From: BC, Canada
Registered: 2011-04-20
Posts: 1,212

Re: [SOLVED] Run systemd service when AC power is connected

Great to hear acpid has solved this for you.  A method without acpid could be to utilize udev rules to run a script to start compton when ac is detected and kill compton when ac is unplugged.

Offline

Board footer

Powered by FluxBB