parsec

powerbutton clean shutdown

Recommended Posts

Im not sure if it is possible, but it would be nice if pushing the powerbutton would shut it down cleanly.

/Rene

Share this post


Link to post
Share on other sites

I don't know if the power button can be tied to a script, but ctrl-alt-del certainly can.

http://lime-technology.com/wiki/index.php?title=Binding_powerdown_script_to_ctrl-alt-del_for_headless_shutdown

It looks like the power button can be tied to the shutdown script, if your motherboard implements an ACPI interface... Check out this thread for ideas:

http://www.murga-linux.com/puppy/viewtopic.php?search_id=2036620187&t=21768

 

It appears as if we already have the acpi daemon running, so all we would need is the "button" module, and then add the file in /etc/acpi/events/power to describe what to do when the power button is pressed.  Right now, only a "default" action is defined in /etc/acpi/events.  It invokes /etc/acpi/acpi_handler.sh and it in turn invokes /sbin/init 0 when the power event is detected.

 

I'm guessing it should invoke WeeboTech's powerdown script.

 

 

Joe L.

Share this post


Link to post
Share on other sites

Sorry if this is in the wrong thread:

 

Is 'shutdown -h now' in a telnet session considered a clean shutdown?

Share this post


Link to post
Share on other sites

Sorry if this is in the wrong thread:

 

Is 'shutdown -h now' in a telnet session considered a clean shutdown?

no

Share this post


Link to post
Share on other sites

so all we would need is the "button" module, and then...

It appears as if the "button" module is compiled into the kernel, we might just need to add the event handler script.

 

Joe L.

Share this post


Link to post
Share on other sites

Another possibility is the poweroff_cmd kernel parameter, as seen in /proc/sys/kernel

 

It is currently set to /sbin/poweroff

 

It can probably be set to anything we want using:

sysctl -w kernel.poweroff_cmd="our_own_shutdown.sh"

 

Joe L.

 

Share this post


Link to post
Share on other sites

Sorry if this is in the wrong thread:

 

Is 'shutdown -h now' in a telnet session considered a clean shutdown?

no

 

Thanks! I stopped doing that a while back in case this was the answer.

Share this post


Link to post
Share on other sites

so is there anymore to this....  I really like this idea b/c my unraid is headless and sometimes I forget to shutdown the server before I shutdown my other desktops.  It would be nice to just press the power button.

 

I'm pretty sure I've seen WeeboTech's powerdown script.  Where do I put it? and where do I have to be to type in the sysctl -w command below?

 

Thanks,

 

J

Share this post


Link to post
Share on other sites

I have not tried this, however it would be put into your /boot/config/go script as

 

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

 

After you install my powerdown script into /sbin

 

I would suggest if you try this, first stop the array manually from the browser, then give it a try.

 

This way your file systems wont come up dirty if something goes wrong.

 

Share this post


Link to post
Share on other sites

This is working I tested it out tonight on my ABIT AB9 PRO.

 

Here is my initialization script for the /sbin/powerdown command shell posted elsewhere.

 

I install it in /boot/custom/etc/rc.d

and call it in my go script as /boot/custom/etc/rc.d/S30-inittab-powerdown

 

I'll probably rename it in the future or add it as part of the package install.

 

In the meantime we know it's working.

 

#!/bin/bash

COMMAND=/sbin/powerdown
SRC_COMMAND=/boot/custom/bin/powerdown
INSTALL=1

if [ ${DEBUG:=0} -gt 0 ]
   then set -x -v
fi

if [ -f ${SRC_COMMAND} -a ! -f ${COMMAND} -a "${INSTALL:=0}" -gt 0 ]
   then fromdos < ${SRC_COMMAND} > ${COMMAND}
        chmod u=rwx ${COMMAND}
fi

if [ -x ${COMMAND} ];then 
   if ! grep "${COMMAND}" /etc/inittab > /dev/null ; then 
      grep -v 'ca::ctrlaltdel:/sbin/shutdown' < /etc/inittab > /etc/inittab.tmp
      echo "ca::ctrlaltdel:/sbin/powerdown" >> /etc/inittab.tmp
      mv /etc/inittab.tmp /etc/inittab
      /sbin/telinit q
   fi
fi

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

 

Share this post


Link to post
Share on other sites

I was reading through this trying to figure everything out and thought I would sum it up here clear up any confusion when someone else is reading this.

 

Install the Shutdown script as listed here;

 

http://lime-technology.com/wiki/index.php?title=Powerdown_script

 

Then, install the script to shutdown with ctrl-alt-del from here;

 

http://lime-technology.com/wiki/index.php?title=Binding_powerdown_script_to_ctrl-alt-del_for_headless_shutdown

 

and finally just add this line to the end of the ctrl-alt-del script when installing it;

 

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

 

You can now use ctrl-alt-del script or the power button to shut down Unraid.

 

Maybe it would help to update that wiki page to add this line and call it "Making ctrl-alt-del and the power button cleanly shut down Unraid"

 

One last question - Is it possible to have one script to do all of the above that can be run with a single line in the go script such as "fromdos < /boot/custom/etc/rc.d/S30-inittab-powerdown | sh " to make this much simpler for newbies to use? I could probably figure it out but it would take me hours to do what some of the more knowledgeable here could in minutes.

 

Peter

 

 

Share this post


Link to post
Share on other sites
Is it possible to have one script to do all of the above that can be run with a single line in the go script

 

I will eventually create a package so that installing from pkginstall will handle all of the associated setups.

 

1. Install powerdown script

2. Update /etc/inittab for ctrl-alt-del support

3. Update kernel.poweroff_cmd for power button control

Share this post


Link to post
Share on other sites

That would be cool. I'm guessing the best time to create a new package is when (or if) Unraid ends up with the plugin directory structure you were discussing in another thread.

 

Peter

 

Share this post


Link to post
Share on other sites

The powerdown package does not really require the plugin architecture.

 

Each package has an install script called doinst.sh that is run upon installation.

So I have to take my startup script and put it into the doinst.sh script.

 

I've just been a bit lazy about it.

 

Although I've updated the power down script recently to support -r switch for reboot and dated saved syslogs.

 

I've been busy with some kernel patches to test out a possible new feature that will have everyone coo'ing.  ;D <-- teaser

 

Share this post


Link to post
Share on other sites

OK, I have the powerdown script working and cntr-alt-del working. I can use both and the array restarts without beginning a parity check.

 

However, the power button does not work. I get "kernel.poweroff_cmd = /sbin/powerdown" as the last line before the "welcome to Linux.." line but when I use the power button a parity check is started when I boot it back up. That last line seems to indicate it's attaching the power down script to the button.  I don't really know how I could save a log showing what's happening during the shutdown.

 

Any ideas?

 

The power button was the part I really wanted to work. I wanted the option of just hitting the power button to shut down because my server is headless.

 

 

 

Peter

Share this post


Link to post
Share on other sites

Some of this depends on the hardware and how it is trapped.

I tested it on one of my machines and it worked.

I think the hardware/bios setting on mine is at 4 seconds.

 

type in

sysctl kernel.poweroff_cmd

if it comes back with

kernel.poweroff_cmd = /sbin/powerdown

 

Then it's set.

 

It should save the syslog in /boot/logs or /boot/custom/log

 

You might have to attach a head to this to get to the bottom of the situation.

 

 

Share this post


Link to post
Share on other sites

I had already hooked up a monitor and keyboard just to see what's going on and install these scripts.

 

kernel.poweroff_cmd = /sbin/powerdown is definitely set

 

The script is not running with the power button, it's not shutting down cleanly and it's not saving a syslog when I press the power button. Pressing the power button causes the broadcast message from the shutdown command to appear immediately.

 

Running /sbin/powerdown there is a pause before the broadcast message caused by the shutdown command at the end of the script. When I type /sbin/powerdown it powers off cleanly and saves a syslog.

 

The power button gives the following;

INIT: switching to runlevel: 0

INIT: sending processes the TERM signal

INIT: sending processes the KILL signal

INIT: ...executes /etc/rc.d/rc.0 which is a link to the script /etc/rc.d/rc.6.

 

I finally deleted rc.6 so it would stop and I could take a better look at what was happening. I first though rc.6 was ran immediately.

 

From what it's doing, I think it is too late to put the powerdown commands into this rc.6 script some how. I'm really rusty with Linux - it's been about 10 years so I'm now stuck.

 

Peter

 

Share this post


Link to post
Share on other sites

If your case has a reset button, you could wire that to the motherboard power pins. Any momentary switch can be used

as a power button on an ?TX motherboard. If it's in a cabinet, you could even get fancy and wire the switch externally,

with a quick connect to disconnect the switch for maintenance. Just make sure the switch is protected from accidental

pushes. :)

 

Jonathan

Share this post


Link to post
Share on other sites
I finally deleted rc.6 so it would stop and I could take a better look at what was happening. I first though rc.6 was ran immediately.

 

From what it's doing, I think it is too late to put the powerdown commands into this rc.6 script some how. I'm really rusty with Linux - it's been about 10 years so I'm now stuck.

 

It seems that the kernel.poweroff_cmd is not trapped, accepted or working correctly.

 

Deleting rc.6 would not be the answer.

 

Perhaps renaming the /sbin/poweroff command and copying the powerdown command to poweroff would reveal something.

(although it might cause a loop, I'm not sure at this point)

 

 

At this point I'm a bit baffled because I tested it on 2 of my machines and there was no issue.

 

 

When testing I would suggest, bring up the system.  stop the array from the web interface.

Make sure all filesystems are unmounted via telnet.

This way you will not have to do a parity check upon startup.

 

Then do any powerbutton testing.

Watch the console. At the very least there should be messages from the powerdown command and syslog should be saved.

When you come up you can look in /boot/logs to see if the syslog was saved.

 

Share this post


Link to post
Share on other sites

My initial investigation of the power button resulted in me finding:

It looks like the power button can be tied to the shutdown script, if your motherboard implements an ACPI interface... Check out this thread for ideas:

http://www.murga-linux.com/puppy/viewtopic.php?search_id=2036620187&t=21768

 

It appears as if we already have the acpi daemon running, so all we would need is the "button" module, and then add the file in /etc/acpi/events/power to describe what to do when the power button is pressed.   Right now, only a "default" action is defined in /etc/acpi/events.  It invokes /etc/acpi/acpi_handler.sh and it in turn invokes /sbin/init 0 when the power event is detected.

 

I'm guessing it should invoke WeeboTech's powerdown script.

 

I think the statement above got forgotten about.  The kernel parameter seemed to work for some, but in addition I would try something else.

Instead of deleting the existing "defaults" script and adding a "power" script, I decided it it easier to modify the acpi_handler.sh script.

To do this I added a "sed" (stream editor) command to the bottom of the script invoked by "go" to set the power button handling:

 

sed -i -e "s/init 0/powerdown/" /etc/acpi/acpi_handler.sh   

 

In /etc/acpi/acpi_handler.sh it changes this:

case "$1" in

  button)

    case "$2" in

      power) /sbin/init 0

...

to this:

case "$1" in

  button)

    case "$2" in

      power) /sbin/powerdown

...

This way, if the button event is detected, it is very likely to go to the powerdown script of commands, and not to run level 0. (shutdown)

 

My script invoked by "go" is named  S30-inittab-powerdown

It now contains the following contents:

 

#!/bin/bash

 

COMMAND=/sbin/powerdown

SRC_COMMAND=/boot/custom/bin/powerdown

INSTALL=1

 

if [ ${DEBUG:=0} -gt 0 ]

   then set -x -v

fi

 

if [ -f ${SRC_COMMAND} -a ! -f ${COMMAND} -a "${INSTALL:=0}" -gt 0 ]

   then fromdos < ${SRC_COMMAND} > ${COMMAND}

        chmod u=rwx ${COMMAND}

fi

 

if [ -x ${COMMAND} ];then

   if ! grep "${COMMAND}" /etc/inittab > /dev/null ; then

      grep -v 'ca::ctrlaltdel:/sbin/shutdown' < /etc/inittab > /etc/inittab.tmp

      echo "ca::ctrlaltdel:/sbin/powerdown" >> /etc/inittab.tmp

      mv /etc/inittab.tmp /etc/inittab

      /sbin/telinit q

   fi

fi

 

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

sed -i -e "s/init 0/powerdown/" /etc/acpi/acpi_handler.sh         #<-- new line added to replace init 0 with powerdown

 

My server does not have a power button, so somebody with one will need to test.  I do know one thing for sure, the "init 0" to power off the machine will not happen.

 

Joe L.

PS.

Funny story... a long time ago I was acting as system admin for a large unix machine at AT&T doing development of an application for a customer you might have heard of ... the New-York-Stock-Exchange.  We were in our offices in the suburbs of NYC and had assembled a team of about 30 developers.  To add new "terminals" as additional team members were added you had to enable or add a line to the /etc/inittiab file and then type "init q" to have the "init" process re-read the /etc/inittab file.    (this was before TCP/IP networking, all workstations were serial terminals)

 

Did you know the "q" key and the "1" key are almost adjacent on most keyboards, and that if you type really fast you can hit the "1" instead of the "q"?

Well, I learned it that day. 

 

I was in a hurry, fat-fingered the keyboard, and brought the machine to "single user mode" (init 1) stopping all the development by a whole team of analysts and programmers.  Can you say "Sorry...." (I was embarrassed... not sure if most of them ever realized why the machine went off line that morning.)

Share this post


Link to post
Share on other sites


Copyright © 2005-2017 Lime Technology, Inc. unRAID® is a registered trademark of Lime Technology, Inc.