Bind power button press to another command


Recommended Posts

Hi there,

 

I am trying to run a custom command whenever the power button of the server is pressed. 

In particular, I'm looking for a docker run command.

 

I found at this page that one way to do that is

 sysctl -w kernel.poweroff_cmd="/sbin/powerdown"

 

So I replaced "/sbin/powerdown" with "docker run ..."

But it is not working and the system is shutting down normally.

 

The page also says that:

Quote

Some motherboards support this, and some don't. Try it and see.

 

Guess mine doesn't. 

 

Is there any other way to do this?

Link to comment

So...Nobody? 

I started messing up with /etc/acpi/acpi_handler.sh and I have replaced "/sbin/init 0" with "docker run ... "

The system does not turn off anymore when I press the power button, but the docker command isn't being executed either.

I do not know how this works and how to debug it, so any help will be appricated.

Link to comment
14 minutes ago, Alby24 said:

So...Nobody? 

I started messing up with /etc/acpi/acpi_handler.sh and I have replaced "/sbin/init 0" with "docker run ... "

The system does not turn off anymore when I press the power button, but the docker command isn't being executed either.

I do not know how this works and how to debug it, so any help will be appricated.

You may need to include the full path 

 

which docker

 

should provide full path

Link to comment

Thanks  for your repy.

 

I see what you mean, I found out that docker is located at:

EDIT: this is the right path

/usr/bin/docker

 

But I dont know the exact path to the executable.

 

Isn't there a simple command that I can replace "/sbin/int 0" with, in order to see if this approach can work?

Edited by Alby24
Link to comment
2 hours ago, SimonF said:

Is /usr/bin/docker on my system.

 

Yes you are correct, now I can use the command.

Anyway it doesn't seem like it is being executed when I press the power button.

Does acpi logs anything? 
 

Here is my entire acpi_handler.sh script:

#!/bin/sh
# Default acpi script that takes an entry for all actions

IFS=${IFS}/
set $@

case "$1" in
  button)
    case "$2" in
      power) /usr/bin/docker pull alpine:latest
         ;;
      *) logger "ACPI action $2 is not defined"
         ;;
    esac
    ;;
  *)
#    logger "ACPI group $1 / action $2 is not defined"
    ;;
esac

 

Link to comment

Have you tried adding a ‘logger’ command in front of the ‘docker run’ one, and another one after so you get entries into the syslog?  That would at least confirm that point is being reached and the command tried.   Not sure if you should also redirect any output from the docker run command anywhere to see if is failing for some reason :( 

Link to comment

Man if you aren't sure, I really don't know what to do, I know very little about this syntax.

Do you mean writing something like this?

 case "$2" in
      power) logger "before run" && /usr/bin/docker pull alpine:latest && logger "after run"
         ;;

 

Plus, where can I find the log?

 

Thanks again.

Edited by Alby24
Link to comment
4 minutes ago, Alby24 said:

Man if you aren't sure, I really don't know what to do.

Do you mean writing something like this?

 case "$2" in
      power) logger "before run" && /usr/bin/docker pull alpine:latest && logger "after run"
         ;;

 

Plus, where can I find the log?

 

Thanks again.

You can put them on separate lines so they get executed in order (as long as they are all before the ;; that terminates the case.

 

The syslog is accessible via the 'Log' icon at the top right of the Unraid GUI.  You can bring it up before pressing the power button and thus see the entries appearing if they are being executed

Link to comment

Indeed, and that is weird, since the standard "/sbin/init 0" was being reached.
Plus, the system doesnt shut down with the button anymore, that implies that I have modified the correct file. 

We're missing something for sure, but I have no idea what

Edited by Alby24
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.