Reading "What is the difference between Halt and Shutdown commands?" , I generally have an idea what does the command shutdown does, with or without -h/-r options.
The "halt" command performs power off of the system to run-level 0 of
the system.The "shutdown" command performs a power off of the system to run-level
1 without -h or -r command.
What about the command "poweroff" does it goes into run-level 0 or 1 ?
Is this the only main difference between these three commands?
Best Answer
And now, the systemd answer.
You're using, per the tag on your question, Red Hat Enterprise Linux. Since version 7, that has used systemd. None of the other answers are correct for the world of systemd; nor even are some of the assumptions in your question.
halt
command from the van Smoorenburg "System 5"init
utilities./sbin/halt
is a symbolic link to/sbin/reboot
; that's not true with systemd. There is no separatereboot
program at all.halt
orreboot
invoke ashutdown
program with command-line arguments; they are also not true with systemd. There is no separateshutdown
program at all.Every system management toolset has its version of these utilities. systemd, upstart, nosh, van Smoorenburg
init
, and BSDinit
all have their ownhalt
,poweroff
, and so forth. On each their mechanics are slightly different. So are their manual pages.In the systemd toolset
halt
,poweroff
,reboot
,telinit
, andshutdown
are all symbolic links to/bin/systemctl
. They are all backwards compatibility shims, that are simply shorthands for invoking systemd's primary command-line interface:systemctl
. They all map to (and in fact are) that same single program. (By convention, the shell tells it which name it has been invoked by.)targets, not runlevels
Most of those commands are shorthands for telling systemd, using
systemctl
, to isolate a particular target. Isolation is explained in thesystemctl
manual page (q.v.), but can be, for the purposes of this answer, thought of as starting a target and stopping any others. The standard targets used in systemd are listed on thesystemd.special
(8) manual page.The diagrams on the
bootup
(7) manual page in the systemd toolset, in particular the last one, show that there are three "final" targets that are relevant here:halt.target
— Once the system has reached the state of fully isolating this target, it will have called thereboot(RB_HALT_SYSTEM)
system call. The kernel will have attempted to enter a ROM monitor program, or simply halted the CPU (using whatever mechanism is appropriate for doing so).reboot.target
— Once the system has reached the state of fully isolating this target, it will have called thereboot(RB_AUTOBOOT)
system call (or the equivalent with the magic command line). The kernel will have attempted to trigger a reboot.poweroff.target
— Once the system has reached the state of fully isolating this target, it will have called thereboot(RB_POWER_OFF)
system call. The kernel will have attempted to remove power from the system, if possible.These are the things that you should be thinking about as the final system states, not run levels. Notice from the diagram that the systemd target system itself encodes things that are, in other systems, implicit rather than explicit: such as the notion that each of these final targets encompasses the
shutdown.target
target, so that one describes services that must be stopped before shutdown by having them conflict with theshutdown.target
target.systemctl
tries to send requests tosystemd-logind
when the calling user is not the superuser. It also passes delayed shutdowns over tosystemd-shutdownd
. And some shorthands triggerwall
notifications. Those complexities aside, which would make this answer several times longer, assuming that you are currently the superuser and not requesting a scheduled action:systemctl isolate halt.target
has the shorthands:shutdown -H now
systemctl halt
halt
systemctl isolate reboot.target
has the shorthands:shutdown -r now
telinit 6
systemctl reboot
reboot
systemctl isolate poweroff.target
has the shorthands:shutdown -P now
telinit 0
shutdown now
systemctl poweroff
poweroff
systemctl isolate rescue.target
has the shorthands:telinit 1
systemctl rescue
systemctl isolate multi-user.target
has the shorthands:telinit 2
telinit 3
telinit 4
systemctl isolate graphical.target
has the shorthand:telinit 5
After parsing the various differing command-line syntaxes, these all eventually end up in the same code paths inside the
systemctl
program.Notes:
shutdown now
has been to switch to single-user mode. This is not the case with systemd.rescue.target
— single-user mode being renamed rescue mode in systemd — is not reachable with theshutdown
command.telinit
really does wholly ignore all of thoserunlevelN.target
anddefault.target
symbolic links in the filesystem that the manual pages describe. The aforegiven mappings are hardwired into thesystemctl
program, in a table.--force
option to thehalt
,reboot
, andpoweroff
commands is the same as saying--force --force
to thesystemctl halt
,systemctl reboot
, andsystemctl poweroff
commands. This makessystemctl
try to callreboot()
directly. Normally it just tries to isolate targets.telinit
is not the same asinit
. They are different programs in the systemd world, the latter being another name for thesystemd
program, not for thesystemctl
program. Thesystemd
program is not necessarily compiled with any van Smoorenburg compatibility at all, and on some systemd operating systems complains about being invoked incorrectly if one attemptsinit N
.Further reading
systemctl
. systemd manual pages. freedesktop.org.systemd.special
. systemd manual pages. freedesktop.org.bootup
. systemd manual pages. freedesktop.org.init
. nosh Guide. Softwares.