2

I've made a tkinter app to handle the power button press, but it doesn't launch when it should. Although if I use echo hello>>~/junk as the acpi action, it adds a hello to junk for every power button press. Adding an exec or a dot before the action command (to execute it in the current shell) didn't help. How can I make it work?

Edit:

Ok I got that to work with the help of mikejonesey, but one more issue appeared after that. The script is called with root privilages, even with the suid bit set on the script, so I have to enter the root passwd at the xlock screen to resume.

Edit 2:

It looks like I can launch the app from an acpi events file like this one: event=button/power PBTN action=export DISPLAY=:0 && export XAUTHORITY=/home/saga/.Xauthority &&/home/saga/bin/shutdownprompt

but not from a handler script called from an event file:

button/power ) if [ "$2" = "PBTN" ];then export DISPLAY=:0 && export XAUTHORITY=/home/saga/.Xauthority && /home/saga/shutdownprompt; fi ;;

This is a clipping from main handler script called on every key press. What is the problem here?

saga
  • 1,381
  • 11
  • 36

2 Answers2

2

you need to add some stuff to use X;

I use these in a cronjob (it's the same thing...)

#*/15 * * * *   export DISPLAY=:0.0 && export XAUTHORITY=/home/mike/.Xauthority && /home/mike/bin/defunct-check.sh

This should work from any script.

mikejonesey
  • 1,950
  • 10
  • 17
  • Works on single-user systems, but woefully inadequate in multi-user environments. – Wyatt Ward Sep 21 '18 at 16:45
  • @Wyatt8740 people like you are why I stopped posting here... nothing of value to add, only negativity... add your own solution if you have a better one... and you forgot the multi-user multi-display system... – mikejonesey Sep 24 '18 at 23:00
  • 1
    I'm not writing an answer because I don't have an answer. I found this post because I was _also_ searching for an answer. BUT I'm making a comment as a warning to others who decide to use this solution, to remind them it's not multi-user safe. If you're the only one on the computer, by all means go ahead. (Also I appreciate being called worthless. Real mature.) – Wyatt Ward Sep 27 '18 at 17:27
0

It seems like there should be an easy solution to this problem. I myself was trying to do something similar: run a screen capture program (flameshot) when a keyboard button is pressed.

In the end, the solution that worked for me was to create a FIFO (named pipe) in my home directory called .acpi_execute.pipe. I then use the following script, called .acpi_execute.

#!/usr/bin/bash

while true ; do
    while IFS='' read command ; do
        $command
    done < ~/.acpi_execute.pipe
done

I prefer i3 over GNOME, KDE, etc, due to it's performance (very lightweight) and tiling nature. At the bottom of my i3 configuration file ~/.config/i3/config I have:

exec ~/.acpi_execute

This allows the script to be run whenever my Desktop Environment starts (whether directly on the machine or via a remote VNC connection).

Be aware that the above has security implications. For example, if anyone can write to the above pipe, they can execute arbitary commands as you. This would obviously be bad. Giving the FIFO 600 permissions, owned by yourself, reduces this risk. You can also limit the commands possible. That is, don't execute $command, but rather the specific command you want (e.g., flameshot gui).

magnus
  • 439
  • 1
  • 4
  • 12