You are not logged in.

#1 2014-05-04 10:48:16

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

[solved] Correct way to start systemd service from udev rule

Hi there,

I used to use the following rule to start a service from an udev rule:

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/bin/systemctl start powersaving.service"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/bin/systemctl start powersaving.service"

This rule successfully runs the service. I changed this to the - what I believe - correct way:

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", TAG+="systemd", ENV{SYSTEMD_WANTS}="powersaving.service"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", TAG+="systemd", ENV{SYSTEMD_WANTS}="powersaving.service"

This rule no longer runs the service, nor is there any log output. Am I missing something?

The respective service file:

[Unit]
Description=Run powersaving script depending on AC state

[Service]
Type=oneshot
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/orschiro/.Xauthority
ExecStart=/usr/bin/powersaving

[Install]
WantedBy=multi-user.target

Last edited by orschiro (2014-05-05 11:25:07)

Offline

#2 2014-05-04 15:21:03

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: [solved] Correct way to start systemd service from udev rule

TAG is not a list; so
TAG+="systemd"
is not correct.

Sorry I answered too quickly.

Last edited by berbae (2014-05-04 15:27:22)

Offline

#3 2014-05-04 20:48:14

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] Correct way to start systemd service from udev rule

This one works, too:

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/bin/systemctl --no-block start powersaving.service"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/bin/systemctl --no-block start powersaving.service"

Offline

#4 2014-05-04 21:14:46

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: [solved] Correct way to start systemd service from udev rule

If that works stay with it.
Is not 'ENV{SYSTEMD_WANTS}=' for .device units only? (not sure)
See 'man systemd.device'.

Offline

#5 2014-05-05 11:24:54

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] Correct way to start systemd service from udev rule

Good point. So I would have to bind my service specifically to a device unit - in that case the power system - ?

In any case, I will stick to the working variant for now.

Thanks for your help!

Offline

Board footer

Powered by FluxBB