You are not logged in.

#1 2022-08-14 14:07:47

innerpeace
Member
Registered: 2022-06-23
Posts: 4

[SOLVED] how to run script succesful on udev rule.

I have written a script to change my display brightness level depending on the power state ( battery or AC) called backlightAlter.sh.
It's content has:

#!/usr/bin/sudo bash
if [[ $1 == true ]]
then
    echo 174 > /sys/class/backlight/intel_backlight/brightness
elif [[ $1 == false ]]
then
    echo 454 > /sys/class/backlight/intel_backlight/brightness
else
    echo Unknow parameter!
fi


And an udev rule file called power_save.rules with:

ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/innerpeace/.Xauthority", RUN+="sudo  /usr/local/bin/backlightAlter.sh true"
ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/innerpeace/.Xauthority", RUN+="sudo  /usr/local/bin/backlightAlter.sh false"

When I plugged or unplugged the power supply the systemd-udev.service display this:

AC: Process 'sudo /usr/local/bin/backlightAlter.sh false' failed with exit code 1.

Does anyone make me out!.
P/S: when I try to run backlightAlter.sh from the terminal it worked well, but not for from udev.

Last edited by innerpeace (2022-08-15 12:29:32)

Offline

#2 2022-08-14 22:48:27

dogknowsnx
Member
Registered: 2021-04-12
Posts: 648

Re: [SOLVED] how to run script succesful on udev rule.

You don't need '#!/usr/bin/sudo bash' shenanigans - simply use one of these, and wrap the commands for increasing/decreasing the backlight each with a simple script which you then reference in your udev rule.
Providing extra arguments is not necessary here, since you already have 'ATTR{online}==' as the differentiating factor...

Example:
udev rule:

ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/innerpeace/.Xauthority", RUN+="/usr/local/bin/dec-backlight.sh"
ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/innerpeace/.Xauthority", RUN+="/usr/local/bin/inc-backlight.sh"

dec-backlight.sh using the light utility:

#!/usr/bin/env bash

light -S 50

inc-backlight.sh, resepectively:

#!/usr/bin/env bash

light -S 80

Reason for editing: utitily > utility

Last edited by dogknowsnx (2022-08-16 11:56:36)


Notifications for Arch Linux package updates
RI - Rest your Eyes and Self

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

#3 2022-08-15 00:23:34

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

Re: [SOLVED] how to run script succesful on udev rule.

You'll be far better off using something closer to your current script than the tools linked above as many of those require a X11 display connection which you will not have with a udev-executed script.  Those that don't require this will end up just doing what your script does, but they'll have a whole bunch of gymnastics around it which just opens up more potential for errors.

But yes, sudo is complete nonsense in this context, both in the command line given to udev and in the shebang of the script; remove both of those.  The udev rule also does not need any DISPLAY or XAUTHORITY environment variables as nothing in your script requires an X11 display connection (which is best).  You can also just put the value as the parameter to the script and the script itself can be just one line:

#!/bin/sh

echo ${1:-454} > /sys/class/backlight/intel_backlight/brightness

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2022-08-15 12:05:44

innerpeace
Member
Registered: 2022-06-23
Posts: 4

Re: [SOLVED] how to run script succesful on udev rule.

Thank you! dogknowsnx, you are right but I prefer the way of Trilby, it worked perfectly! Thank Trilby.

Last edited by innerpeace (2022-08-15 12:28:20)

Offline

#5 2022-08-15 12:25:29

2ManyDogs
Forum Moderator
Registered: 2012-01-15
Posts: 4,645

Re: [SOLVED] how to run script succesful on udev rule.

Please remember to mark your thread [SOLVED] (edit the title of your first post).


How to post. A sincere effort to use modest and proper language and grammar is a sign of respect toward the community.

Offline

Board footer

Powered by FluxBB