You are not logged in.

#1 2012-10-23 20:33:24

apokatastasis
Member
Registered: 2012-08-23
Posts: 5

systemd backlight script

Not sure if this should go here or in scripting (the perils of assuming delineated categories and all that), but anyway...

A few days ago, I finally figured out how to control backlight brightness on my laptop (after a year of using Ubuntu, Crunchbang, and Arch on it), after which I spared no time setting up an event handler in ACPI so that brightness would switch between 50 and 976 (the maximum) when AC power was disconnected and connected respectively.

All of that went well, but unfortunately, there is a slight problem: If I boot with AC disconnected, the brightness stays at 976 and doesn't go down to 50 until either I connect and disconnect AC or alter the brightness file myself. The idea I had involved writing a small bash script which got current AC connection status (from /sys/class/power_supply/AC/online) and would change the brightness file to read 50 if online read 0 (script below). Good, that all worked well, I rebooted the computer and tested the script and it did what it was meant to do.

So... the next thing to do was to somehow execute that script automatically. I immediately thought of making either a cronjob or adding it .bashrc or something similar but in my case, this script only needs to be run once... at boot. Well, by pure chance, this was on the very same day that I updated the system only to find that alsa and syslog-ng failed at boot, prompting me to look into the recent updates and Arch init... long story short, I updated my entire system to use systemd natively and got rid of sysinit. Now all I had to do was write a script for systemd that would invoke the backlight script I made earlier (script below). In the interest of keeping this post from becoming any more lengthy (sorry about that), I will merely say: it fails at boot. I did #systemctl enable backlight.service so I assume I scripted something (in the systemd script) wrong.

/home/casey/bin/backlight

#!/bin/bash

BOOL = $(cat /sys/class/power_supply/AC/online) # 0 or 1

if  [ $BOOL != 1 ]; then
     $(cat /etc/backlight.value > /sys/class/backlight/intel_backlight/brightness) #backlight.value is merely a textfile which read "50"
fi

/etc/systemd/system/backlight.service (I pieced together what I thought was necessary from here)

[Unit]
Description=Determines if AC power exists at boot

[Service]
Type=oneshot
ExecStart=/home/casey/bin/backlight

[Install]
WantedBy=multi-user.target

(As an aside, I do have to say that I quite like systemd and that will probably only continue to increase as I learn more about it)

I do realize there may be a better way to do this, although my primary concern is just coming up with any solution.


Arch/dwm

Offline

#2 2012-10-24 00:57:25

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,412

Re: systemd backlight script

I have my backlight set at boot with a udev rule.  I tried tmpfiles.d first, but found it to be a bit racy.  I would imagine that because udev is also aware of your system's a/c power status, you could also have it adjust your backlight when you [un]plug.  Of course, if you are using acpid for other things, then it would make sense to simply use that.  But I was able to rid myself of acpid altogether with various things.

Offline

#3 2012-10-24 02:09:05

apokatastasis
Member
Registered: 2012-08-23
Posts: 5

Re: systemd backlight script

WonderWoofy wrote:

I have my backlight set at boot with a udev rule.  I tried tmpfiles.d first, but found it to be a bit racy.  I would imagine that because udev is also aware of your system's a/c power status, you could also have it adjust your backlight when you [un]plug.  Of course, if you are using acpid for other things, then it would make sense to simply use that.  But I was able to rid myself of acpid altogether with various things.

Heh, maybe once I get everything running smoothly I'll look into optimizing it more. After all, if I make it as sleek as possible now, what do I do for fun later?

Care to share your udev rule for the backlight?


Arch/dwm

Offline

#4 2012-10-24 02:22:08

bzhb
Member
Registered: 2010-11-17
Posts: 32

Re: systemd backlight script

Try to add :
  After=systemd-udevd.service

in the [Unit] section

Offline

#5 2012-10-24 06:08:42

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,412

Re: systemd backlight script

ACTION=="add", KERNEL=="acpi_video0", SUBSYSTEM=="backlight", SUBSYSTEMS=="pci", DRIVERS=="i915", ATTR{brightness}="2"

Offline

Board footer

Powered by FluxBB