How to make linux power off when halt is run?

纵饮孤独 提交于 2019-12-01 18:35:48

You should use poweroff command or halt -p. As per man 8 halt, halt command (with no arguments) doesn't guarantee to power off your machine. Reasons are described here:

halt was used before ACPI (which today will turn off the power for you)*. It would halt the system and then print a message to the effect of "it's ok to power off now". Back then there were physical on/off switches, rather than the combo ACPI controlled power button of modern computers.

*These days halt is smart enough to automatically call poweroff if ACPI is enabled. In fact, they are functionally equivalent now.

As you can see from halt tool source code, it issues reboot() system call with cmd = RB_POWER_OFF = LINUX_REBOOT_CMD_POWER_OFF.

In kernel, that system call is implemented here, and on cmd = LINUX_REBOOT_CMD_POWER_OFF, it calls:
   -> kernel_power_off()
   -> machine_power_off()
   -> pm_power_off()

On Debian/Ubuntu systems (I don't know if in other Linux OSes will work this), if you want the halt command does a poweroff you can create this file:

/etc/default/halt

Write this content of that file:

# Default behaviour of shutdown -h / halt. Set to "halt" or "poweroff".
HALT=poweroff

As the comment says, you can set now how halt should work. If you set it to halt it will only shutdown the computer without powering off. If you set poweroff it will work like as the poweroff command powering off the computer after closing all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!