Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Bind power button press to another command

Featured Replies

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?

  • Author

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.

  • Community Expert
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

  • Author

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

  • Community Expert
14 minutes ago, Alby24 said:
/var/lib/docker

This is the full path 

  • Author

But when I try to run a command like:

/var/lib/docker run alpine:latest

 

This is shown:

bash: /var/lib/docker: Is a directory

 

And nothing is executed

  • Author
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

 

  • Community Expert

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 :( 

  • Author

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

  • Community Expert
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

  • Author

I tried as you said and I believe nothing is being logged at all.

I don't really know what to say...

  • Community Expert
1 hour ago, Alby24 said:

I tried as you said and I believe nothing is being logged at all.

I don't really know what to say...

 

That implies that the code is not being reached in the first place.

  • Author

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

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...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.