59

I've a home server I'm playing around with :) I want to learn server administration...

I have a hard time understanding the difference between shutdown -h now and poweroff -h now My logic tells me it's the same thing...

But with the shutdown, Linux halts, and power stays on - I can see all lights up and cooler rotating. Strange, the only way to turn off my box completely is to use poweroff -h

Any clarification will be appreciated!

Milan
  • 172

2 Answers2

53

The answer is in the poweroff(8) man page:

When called with --force or when in runlevel 0 or 6, this tool invokes the reboot(2) system call itself and directly reboots the system. Otherwise this simply invokes the shutdown(8) tool with the appropriate arguments.

A bit of explanation:

  • The reboot() system call is the kernel function used to reboot, halt or poweroff the machine. It is called reboot for historical reasons, but performs all three functions, depending on parameters that are passed to it.

  • The difference between halt and poweroff is that in halt mode the control is returned to a "ROM monitor" (think BIOS), whereas poweroff simply powers the system board off. (I have never seen this distinction in effect on PC architectures; on Sun machines it's different, though.)

  • So, when the poweroff program is invoked when the Linux system is in runlevel 0 or 6, it will immediately power off the system via the reboot(RB_POWEROFF) system call.

  • In any other case, poweroff will just behave as an alias for shutdown now.

muru
  • 197,895
  • 55
  • 485
  • 740
  • 13
    I'm not so good at reading technicalities. I still don't get the difference. I might be stupid, but for me reboot is reboot. I don't get why power-off has to reboot anything... –  Oct 09 '11 at 22:18
  • @SandroDzneladze I've added an explanation; hope it's more clear now. – Riccardo Murri Oct 10 '11 at 08:51
  • It's awesomely clear :) thanks for your time... really! –  Oct 10 '11 at 15:32
  • 1
    So if poweoff just pulls the power to the system board, isn't this bad? Isn't it just like pulling the plug? No chance for programs to end gracefully? Is there a safe way to shutdown remotely? – Peter White Jun 22 '21 at 12:01
  • 3
    @PeterSnow Normally poweroff is just an alias for shutdown now, so it will initiate a graceful shutdown process. At the end of this process, poweroff --force is called, which actually powers the motherboard off. – Riccardo Murri Jun 23 '21 at 13:52
  • So if poweroff is just shutdown (sometimes?), doesn't that bring us back to the same question? What is the difference between poweroff and shutdown and why do we need both? – AlexPi Nov 03 '22 at 02:58
  • @AlexPi There's two things that you need to do when halting/rebooting/powering off a machine: (1) Signal all processes to terminate and exit, unmount filesystems, etc. and (2) actually telling the kernel to power off or reboot the hardware. Action (1) is the domain of the shutdown command, and action (2) is what commands halt/poweroff/reboot would do. However, for ease of use and to prevent errors, halt/poweroff/reboot will invoke shutdown -h/-P/-r instead of powering off / rebooting immediately. (The latter requires --force and is done by shutdown at the end.) – Riccardo Murri Nov 04 '22 at 13:34
  • @RiccardoMurri where is documented that poweroff is an alias of shutdown now? Is there other reason to have this alias apart of typing less? – Manuel Jordan Aug 24 '23 at 13:53
  • 1
    @ManuelJordan This was explicit in the old SysV man pages, see e.g. https://linux.die.net/man/8/poweroff (man page dated 2009) -- now that all this is managed by systemd commands, the notion is no longer made explicit in documentation which seems to assume that one is already familiar with the SysV equivalent. But basically in systemd all commands translate to systemctl start {halt,poweroff,reboot}.target, immediately in the case of halt/poweroff/reboot, and with a configurable delay in the case of shutdown. – Riccardo Murri Aug 28 '23 at 06:22
  • Thanks for the polite answer. – Manuel Jordan Aug 28 '23 at 12:54
0

To the original question "shutdown" vs. "poweroff": As the names imply, "shutdown" ends in something like "cli(); while(1);" while "poweroff" communicates somehow with the power supply before ending execution in a similar or same loop like "shutdown". (Only for the case that there is a problem with the power supply.)

In case of Raspberry Pi, by default, it is the same. However, there is a standard kernel overlay available, which can be loaded by modifying /boot/config.txt:

dtoverlay = gpio-poweroff

which changes (by default) GPIO26 (Pin 37) to an Output driving High. As this pin is not used by default, and unused pins are inputs without pullup/pulldown, a simple circuitry can cut power supply at High level. Note (for the designer of circuitry): This pin will revert to Low after power cut, of course! So use a flipflop.

As the overlay name "gpio-poweroff" should imply, it is called (somehow by systemd) on "poweroff", not on "shutdown" or "halt".