You are not logged in.

#1 2011-03-12 12:37:37

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

notify-send from acpi scripts

I have looked for quite some time now on how to get a pop-up (notify-send) to work from an acpi event / script sad

For me to understand this (in a simple way (and please correct my logic!)):

  • For events I pass everything into the handler.sh script (where i can do whatever with the button events)

  • When I launch the script manualy (passing the button "code" as an argument) It works!, and i get my pop-up message

  • When I press the actual button, everything works except notify-send

  • Something beyond my understanding must be happening (maybe something regarding dbus, x11)

I tried:

  • adding DISPLAY=:0.0

  • echo $USER returns nothing :S (who does acpi run as?, and can it use the display?)

Someone with a similar error:
https://bugs.launchpad.net/ubuntu/+sour … +bug/92483

sudo /etc/rc.d/acpid restart does fix the issue, but why does it?
It feels sub-optimal having to restart it on every boot hmm

Sorry if my description of this whole thing is lacking, but im new to both acpi, shell scripting, and permissions in general (who is allowed to do what, etc)

thanks in advance

-Julian


When there is nothing left to do. Do nothing.

Offline

#2 2011-03-12 14:14:16

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: notify-send from acpi scripts

Sorry for double posting, but this seemed important:

After i restart the acpid, the command $USER now returns root (which makes sense, since i restarted it as root (On a side note i would like to know how to restart acpid as a user (without using sudo) if possible. This is so that notify-send uses the color theme I have set for the user triggering the acpi event))

I am now wondering about who "owns" acpid when its first launched??

Im guessing it is launched first from:

/etc/rc.conf

DAEMONS=(@network @syslog-ng @acpid netfs @crond dbus wicd)

But I don't know what user this falls under (if any)

Am I even digging at the right place?
Does me backgrounding it have any say? (so many questions tongue)
I thought acpid was to run in the background anyway


When there is nothing left to do. Do nothing.

Offline

#3 2011-03-12 14:57:40

skunktrader
Member
From: Brisbane, Australia
Registered: 2010-02-14
Posts: 1,578

Re: notify-send from acpi scripts

A little hackish, but assuming you are only running a single x session, you could try something like this

/bin/su $(ps aux | grep '[x]init /home' | awk '{print $1}' | head -1) --login -c notify-send .....

Online

#4 2011-03-12 14:58:30

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: notify-send from acpi scripts

It's a daemon that gets run a root. So running notify-send as the root user does nothing. The way to get around it is to run the notify-send command as your user with the Substitute User command:

root@computer # su tom -c "echo $USER"
tom
root@computer #

The -c is explained in the man page; it runs a following command as the previously defined user.

The way I actually do this is to call a script in my bin which does the notification. The script is called using:

# su tom -c "/home/tom/bin/myscript"

Offline

#5 2011-03-12 16:53:17

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: notify-send from acpi scripts

I use this:

notify() {
    title="$1"
    shift 1
    body="$*"

    for pid in $(pgrep 'awesome'); do
        eval $(grep -z ^USER /proc/$pid/environ)
        eval export $(grep -z ^DISPLAY /proc/$pid/environ)
        eval export $(grep -z ^DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ)
        su $USER -c "notify-send \"$title\" \"$body\""
    done
}

Replace awesome with your WM or a program that you will run iff you are in X.


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

#6 2011-03-12 17:26:28

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: notify-send from acpi scripts

@skunktrader and BaconPie:

I have now tried to use su in my handler.sh and whilst it doesn't solve my initial problem (Still have to restart acpid sad ) it does solve the problem of getting messages to have the same color theme as the user smile, so thanks for getting me closer to the solution.

As for not being able to call notify-send using the acpid launched at boot, I am still just as lost hmm

I keep thinking that someone other than root launches rc.conf? (I am so confused about this xD)

info that might be relevant:

I noticed that the volume buttons worked out of the box (gnome), and I'm not sure what "button event manager" It uses but it seems to be unstable (stops working randomly). I would like to replace this with acpid once I find it, but it raised a question "does having 2 of these button event handlers interfere with each other?" (i hope not)

I will now try your solution Stebalien smile (its awesome to get so many solutions to try! thanks!)


When there is nothing left to do. Do nothing.

Offline

#7 2011-03-12 17:35:30

nc
Member
From: Braga, Portugal
Registered: 2009-04-08
Posts: 20

Re: notify-send from acpi scripts

I have a similar problem. I'm trying to lauch a shutdown dialog when I hit the power button. Here is what I have in /etc/acpi/events/power

event=button/power PWRF 00000080 -*
action=su nc -c "sh /home/nc/.scripts/shutdown.sh"

I have also tried whithout the su command, but it's doesn't work as well. If I restart the acpid daemon, it works, but I don't want to restart the daemon everytime I start my WM..

Offline

#8 2011-03-12 18:00:00

rwd
Member
Registered: 2009-02-08
Posts: 664

Re: notify-send from acpi scripts

Please read the wiki before asking, expecially the part about ~/.Xauthority : https://wiki.archlinux.org/index.php/Acpid, and https://wiki.archlinux.org/index.php/Ru … ps_as_root

Last edited by rwd (2011-03-12 18:08:46)

Offline

#9 2011-03-12 21:05:27

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: notify-send from acpi scripts

Again I cannot solve the problem sad, tried your method Stebalien and checked those links you referred to rwd. The info at the link assumes I am root and want to access another users X. But it seems to me like the first time that acpid is run it is not as root? (this is what i dont get)

what points to this is when I try:

echo $USER > testForUserFile

In a script run by the acpid started from boot I get nothing, the file is blank :S, but when I restart acpid (as root ofc), the testForUserFile contains "root".

I think il go investigate how the pc boots & what processes are run under which users etc.

@nc, do you get something similar? would be comforting to know there are others with the same issue smile

It seems to me that the best workaround is to add a "restart acpid" statement to some script that launches on every boot hmm

The search continues tongue


When there is nothing left to do. Do nothing.

Offline

#10 2011-03-12 22:49:31

nc
Member
From: Braga, Portugal
Registered: 2009-04-08
Posts: 20

Re: notify-send from acpi scripts

Yes, I get the same as you. If I restart acpid, the file contains 'root'.

Offline

#11 2011-03-14 18:54:38

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: notify-send from acpi scripts

Sorry I haven't had the time to look into this further over the last 2 days, and probably wont for another 4 days (due some uni assignments). For now I'm just going to live with it (although it bothers me to leave things undone D:, and will most likely continue afterwards)

(I did take a swift glance at this boot process stuff, and it's quite heavy x(, but eventually... smile )

So for anyone else who is also experiencing the same issue (@nc) I apologize for my laziness tongue
Though not trying to (directly) dump the responsibility on someone else, I have to say that I'll be willing to try any solution out there! even the strange ones smile

-Julian

Edit:  I spelled laziness as lazyness yikes

Last edited by JulianNymark (2011-03-14 19:04:07)


When there is nothing left to do. Do nothing.

Offline

#12 2011-04-19 14:02:00

lkraav
Member
Registered: 2011-04-08
Posts: 37

Re: notify-send from acpi scripts

Offline

#13 2011-08-22 12:13:16

dhardy
Member
Registered: 2011-08-22
Posts: 1

Re: notify-send from acpi scripts

Another way of doing this (slightly more generic than Stebalien's method):

notify() {
    # Usage: notify "title" "description" [options to pass to send-notify"
    title="$1"
    body="$2"
    shift 2
    opts="$*"
    
    # Send messages to users listed by the 'users' command, but only once
    # Maybe sh has a better way of doing set operations, but this works:
    SENT=""
    for USER in $(users); do
        case $SENT in
            "$USER") ;;
            *" $USER") ;;
            "$USER "*) ;;
            *" $USER "*) ;;
            # use 'su USER' to send the message
            *) SENT="$SENT $USER"; su $USER -c "notify-send $opts \"$title\" \"$body\"" ;;
        esac
    done
}

Offline

#14 2012-09-26 22:26:13

rephorm
Member
Registered: 2009-06-05
Posts: 3

Re: notify-send from acpi scripts

If you want a generic method for obtaining the active username and display for systems using systemd, see xuserrun.

Offline

Board footer

Powered by FluxBB