Guest User

powerbtn.sh

a guest
Aug 18th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.13 KB | Software | 0 0
  1. #!/bin/sh
  2. # /etc/acpi/powerbtn.sh
  3. # Taken from Debian's 2.0.4-1 diff file.  This version handles KDE4.
  4. # Power Button event handler.
  5. # Checks to see if gnome or KDE are already handling the power button.
  6. # If not, initiates a plain shutdown.
  7.  
  8. # getXuser gets the X user belonging to the display in $displaynum.
  9. # If you want the foreground X user, use getXconsole!
  10. # Input:
  11. #   displaynum - X display number
  12. # Output:
  13. #   XUSER - the name of the user
  14. #   XAUTHORITY - full pathname of the user's .Xauthority file
  15. getXuser() {
  16.         user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
  17.         if [ x"$user" = x"" ]; then
  18.                 startx=`pgrep -n startx`
  19.                 if [ x"$startx" != x"" ]; then
  20.                         user=`ps -o user --no-headers $startx`
  21.                 fi
  22.         fi
  23.         if [ x"$user" != x"" ]; then
  24.                 userhome=`getent passwd $user | cut -d: -f6`
  25.                 export XAUTHORITY=$userhome/.Xauthority
  26.         else
  27.                 export XAUTHORITY=""
  28.         fi
  29.         export XUSER=$user
  30. }
  31.  
  32. # Gets the X display number for the active virtual terminal.
  33. # Output:
  34. #   DISPLAY - the X display number
  35. #   See getXuser()'s output.
  36. getXconsole() {
  37.         console=`fgconsole`;
  38.         displaynum=`ps t tty$console | sed -n -re 's,.*/X .*:([0-9]+).*,\1,p'`
  39.         if [ x"$displaynum" != x"" ]; then
  40.                 export DISPLAY=":$displaynum"
  41.                 getXuser
  42.         fi
  43. }
  44.  
  45. # Skip if we are just in the middle of resuming.
  46. test -f /var/lock/acpisleep && exit 0
  47.  
  48. # If the current X console user is running a power management daemon that
  49. # handles suspend/resume requests, let them handle policy.
  50.  
  51. getXconsole
  52.  
  53. # A list of power management system process names.
  54. PMS="gnome-power-manager kpowersave xfce4-power-manager"
  55. PMS="$PMS guidance-power-manager.py dalston-power-applet"
  56. PMS="$PMS mate-power-manager org_kde_powerdevil"
  57. PMS="$PMS gsd-power"
  58.  
  59. # If one of those is running or any of several others,
  60. if pidof x $PMS > /dev/null ||
  61.     ( test "$XUSER" != "" && pidof dcopserver > /dev/null && test -x /usr/bin/dcop && /usr/bin/dcop --user $XUSER kded kded loadedModules | grep -q klaptopdaemon) ||
  62.     ( test "$XUSER" != "" && test -x /usr/bin/qdbus && test -r /proc/$(pgrep -n kded4)/environ && su - $XUSER -c "eval $(echo -n 'export '; cat /proc/$(pgrep -n kded4)/environ |tr '\0' '\n'|grep DBUS_SESSION_BUS_ADDRESS); qdbus org.kde.kded" | grep -q powerdevil) ; then
  63.     # Get out as the power manager that is running will take care of things.
  64.     exit
  65. fi
  66.  
  67. # No power managment system appears to be running.  Just initiate a plain
  68. # shutdown.
  69. CURRENT_INIT=`ps -p 1 -o comm=`
  70. if [ "$CURRENT_INIT" = "openrc-init" ]
  71. then
  72.         /sbin/openrc-shutdown -p now
  73. else
  74.     #uncommenting the line below allows the script to run when the power button is pressed ig
  75.     #/sbin/shutdown -h now "Power button pressed"
  76.         echo daddy chill | nvim # random string
  77.         nvim # still doesn't work
  78.         echo Daddy chill # nope
  79.         btop # you too? cmon, does powerbtn.sh not write to stdout or receive input from stdin?
  80. fi
  81.  
Advertisement
Add Comment
Please, Sign In to add comment