You are not logged in.

#1 2014-09-24 09:04:26

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

Starting programs depending on AC state

Hi there,

What do you consider the best way to run programs depending on the AC state, i.e. whether the laptop runs on battery or is plugged in?

Example use cases:

* Running my backup program only when I am plugged in
* Running Dropbox when I am mobile, thus on battery

Thanks for sharing your solutions!

Offline

#2 2014-09-25 00:55:12

halocaridina
Member
From: US
Registered: 2014-07-21
Posts: 77

Re: Starting programs depending on AC state

How about a rule under udev?

This is what I use for increasing/decreasing backlight depending on AC state:

cat /etc/udev/rules.d/98-backlight.rules
# Adjust screen brightness according to power state
# 1st rule for when on AC
ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/usr/bin/xbacklight -set 90"
# 2nd rule for when on battery
ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/usr/bin/xbacklight -set 35"

4X Intel(R) Xeon(R) CPU E7-8870 @ 2.40GHz; 256GiB; 11TB EXT4; SL6.8
2X Intel(R) Xeon(R) CPU E5-2680 @ 2.70GHz; 256GiB; 3.4TB EXT4; SL6.8
Thinkpad X250; Broadwell-ULT Core i5; 8GiB, 525GB Crucial_CT525MX3; Arch
Thinkpad T440S; Haswell-ULT Core i7; 12GiB, 512GB SanDisk X210; Arch

Offline

#3 2014-09-25 01:04:27

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,480
Website

Re: Starting programs depending on AC state

I think udev rules would only be useful for reacting to events.  The question sounds more like a conditional test to check the current state:

[[ $(cat /sys/class/power_supply/AC/online) -eq 1 ]] && command-to-run-on-AC-only

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2014-09-25 01:09:16

halocaridina
Member
From: US
Registered: 2014-07-21
Posts: 77

Re: Starting programs depending on AC state

Trilby,

Good point. A conditional test makes more sense in the context of the OP's question.

Halocaridina


4X Intel(R) Xeon(R) CPU E7-8870 @ 2.40GHz; 256GiB; 11TB EXT4; SL6.8
2X Intel(R) Xeon(R) CPU E5-2680 @ 2.70GHz; 256GiB; 3.4TB EXT4; SL6.8
Thinkpad X250; Broadwell-ULT Core i5; 8GiB, 525GB Crucial_CT525MX3; Arch
Thinkpad T440S; Haswell-ULT Core i7; 12GiB, 512GB SanDisk X210; Arch

Offline

#5 2014-09-25 05:05:54

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

Re: Starting programs depending on AC state

Some good ideas. Thank you!

I will test around and report back.

Offline

Board footer

Powered by FluxBB