You are not logged in.

#1 2014-11-25 11:09:48

Dreamkey
Member
Registered: 2010-08-28
Posts: 45

[Solved] Incron doesn't work on /sys files

Hello,

I created a simple script to monitor my battery; it works fine when I test incron with a file in my home or in /tmp, but nothing happens with sysfs files.
I tried as root but the result is the same, and there is nothing in logs.

Here is my incrontab:

/sys/class/power_supply/BAT1/capacity IN_MODIFY /home/dreamkey/scripts/notify-batt.sh
/sys/class/power_supply/ACAD/online IN_MODIFY /home/dreamkey/scripts/notify-batt.sh

Here is my script:

#!/bin/bash

# Change BAT1/ACAD to your system configuration

CAPACITY=`cat /sys/class/power_supply/BAT1/capacity`
STATUS=`cat /sys/class/power_supply/ACAD/online`

if [ $CAPACITY -ge 80 ]; then
    CAP_ICO='full';
elif [ $CAPACITY -ge 60 ]; then
    CAP_ICO='good';
elif [ $CAPACITY -gt 10 ]; then
    CAP_ICO='low';
else
    CAP_ICO='caution';
fi

if [ $STATUS -eq 1 ]; then
    STATUS_STR='AC';
    STATUS_ICO='charging-';
else
    STATUS_STR='battery';
    STATUS_ICO='';
fi

if [ $CAPACITY -eq 10 -o $CAPACITY -eq 5 ]; then
    NOTIF_STR="Battery: $CAPACITY% left";
else
    NOTIF_STR="On $STATUS_STR";
fi

export DISPLAY=:0 #needed for notify-send from cron/incron
notify-send "$NOTIF_STR" -i battery-$CAP_ICO-${STATUS_ICO}symbolic -t 4000

So is it possible to watch files in sysfs? Otherwise, how can I monitor my battery without an active polling?
Thanks.

Last edited by Dreamkey (2014-11-28 00:55:22)

Offline

#2 2014-11-25 11:46:48

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: [Solved] Incron doesn't work on /sys files

I think modalias is what you're looking for.
Didn't try it myself though.
Quote from the wiki:

wiki wrote:

Modalias is a little sysfs trick that exports hardware information to a file named 'modalias'.

Offline

#3 2014-11-25 15:55:17

Dreamkey
Member
Registered: 2010-08-28
Posts: 45

Re: [Solved] Incron doesn't work on /sys files

Thanks for the link, that was interesting to read, but I don't see how it could help.

Offline

#4 2014-11-25 16:40:08

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: [Solved] Incron doesn't work on /sys files

Well, that's what you asked for.., a way to watch files, a battery monitor in this case through sysfs.
It can do that, export hardware information, so the wiki says, how to use it I don know and don't have the time to figure that out.

edit: At least, not now wink

Last edited by qinohe (2014-11-25 16:41:53)

Offline

#5 2014-11-25 22:03:58

Dreamkey
Member
Registered: 2010-08-28
Posts: 45

Re: [Solved] Incron doesn't work on /sys files

I don't see what you're getting at; modalias will export hardware information that will help to load the corresponding module.
But this hardware information is just some kind of unique ID, regardless of the current state of the hardware.

My aim is to have a notification everytime my AC adapter is plugged/unplugged, or when my battery is low.
Those informations are only available through the files I setted incron to watch, but when those files are updated incron doesn't react.

Offline

#6 2014-11-26 12:26:06

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: [Solved] Incron doesn't work on /sys files

Aghh, now I see, you're right it only shows info about the module, but no info from it.
Don't have another idea at the moment. I have one, but that's with conky, and it's actively polling..

Offline

#7 2014-11-28 00:54:47

Dreamkey
Member
Registered: 2010-08-28
Posts: 45

Re: [Solved] Incron doesn't work on /sys files

So incron will never work with sysfs, here is a discussion on a mailing-list.
But there was a solution right in the laptop Arch's wiki, with udev (all other tools I found polled sysfs).

You can find here my script, with the udev rules - I added a zenity countdown when the battery is low.

#!/bin/sh

# Change BAT1/ACAD to your system configuration
# Require udev, libnotify and zenity
# $ cat /etc/udev/rules.d/99-battery.rules
# SUBSYSTEM=="power_supply", ATTR{capacity}=="[0-4]|7|10", RUN+="/usr/bin/sudo -u foo /home/foo/scripts/notify-batt.sh"
# SUBSYSTEM=="power_supply", ATTR{online}=="[01]", RUN+="/usr/bin/sudo -u foo /home/foo/scripts/notify-batt.sh"

export DISPLAY=:0.0 #needed for zenity/notify-send from cron/incron/udev/...
export LANG=en_US.utf8 #needed for zenity from cron/incron/udev/...

CAPACITY=`cat /sys/class/power_supply/BAT1/capacity`
STATUS=`cat /sys/class/power_supply/ACAD/online`

if [ $CAPACITY -ge 80 ]; then
    CAP_ICO='full';
elif [ $CAPACITY -ge 60 ]; then
    CAP_ICO='good';
elif [ $CAPACITY -gt 10 ]; then
    CAP_ICO='low';
else
    CAP_ICO='caution';
fi

if [ $STATUS -eq 1 ]; then
    STATUS_STR='AC';
    STATUS_ICO='charging-';
else
    STATUS_STR='battery';
    STATUS_ICO='';
fi

if [ $CAPACITY -eq 10 -o $CAPACITY -eq 7 ]; then
    NOTIF_STR="Battery: $CAPACITY% left";
elif [ $CAPACITY -le 4 ]; then
    COUNTDOWN=59; # one minute before shutdown (59s because of --auto-close)
    while [[ $COUNTDOWN -gt 0 ]]; do
        echo $(( $COUNTDOWN*100/60 ));
        COUNTDOWN=$(( $COUNTDOWN-1 ));
        sleep 1;
    done | DISPLAY=:0 zenity --progress \
        --title='Shutdown' \
        --text='Low battery, shutting down...' \
        --auto-close

    if [[ $? -eq 0 ]]; then
        sudo poweroff;
    fi
else
    NOTIF_STR="On $STATUS_STR";
fi

notify-send "$NOTIF_STR" -i battery-$CAP_ICO-${STATUS_ICO}symbolic -t 4000

Offline

Board footer

Powered by FluxBB