I would like to do that using the command-line, because sometimes my computer freezes and I need to force a shutdown (I know it's not good to the hardware).
And: What is the difference between Halt and Shutdown commands?
How to shutdown, reboot and logout the system from the command-line
command linelogoutrebootshutdown
Related Solutions
The right thing to do here is to set up bash to prompt for installation, as explained in SamK's answer. I'll answer strictly from a shell usage perspective.
First, the text you're trying to grab is on the command's standard error, but a pipe redirects the standard output, so you need to redirect stderr to stdout.
htop 2>&1 | tail -1
To use the output of a command as part of a command line, use command substitution.
$(htop 2>&1 | tail -1)
The result of the command substitution is split into words and each word is interpreted as a wildcard pattern. Here this happens to do the right thing: this is a command line with words separated by spaces, and there are no wildcard characters.
To evaluate a string as a shell command, use eval
. To treat the result of the command as a string rather than a list of wildcard patterns, put it in double quotes.
eval "$(htop 2>&1 | tail -1)"
Of course, before evaluating a shell command like that, make sure it's really what you want to execute.
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.
- Forget about runlevels; they exist, but only as compatibility shims. The systemd documentation states that the concept is "obsolete". If you're starting to learn this stuff on a systemd operating system, don't start there.
- Forget about the manual page that marcelm quoted; it's not from the right toolset at all, and is a description of another toolset's command, incorrect for systemd's. It's the one for the
halt
command from the van Smoorenburg "System 5"init
utilities. - Ignore the statements that
/sbin/halt
is a symbolic link to/sbin/reboot
; that's not true with systemd. There is no separatereboot
program at all. - Ignore the statements that
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 BSD init
all have their own halt
, poweroff
, and so forth. On each their mechanics are slightly different. So are their manual pages.
In the systemd toolset halt
,poweroff
,reboot
, telinit
, and shutdown
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 the systemctl
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 the systemd.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 the shutdown.target
target.
systemctl
tries to send requests to systemd-logind
when the calling user is not the superuser. It also passes delayed shutdowns over to systemd-shutdownd
. And some shorthands trigger wall
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
- plain unadorned
halt
systemctl isolate reboot.target
has the shorthands:shutdown -r now
telinit 6
systemctl reboot
- plain unadorned
reboot
systemctl isolate poweroff.target
has the shorthands:shutdown -P now
telinit 0
shutdown now
systemctl poweroff
- plain unadorned
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:
- The traditional behaviour of option-less
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.- systemd has no notion of a current run level. The operation of these commands is not conditional upon any "if you are in run-level N".
- The
--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
- Are there any good reasons for halting system without cutting power?
- Why does `init 0` result in "Excess Arguments" on Arch install?
- Stephen Wadeley (2014). "8. Managing Services with systemd" Red Hat Enterprise Linux 7 System Administrators' Guide. Red Hat.
- Lennart Poettering (2013-10-07).
systemctl
. systemd manual pages. freedesktop.org. - Lennart Poettering (2013-10-07).
systemd.special
. systemd manual pages. freedesktop.org. - Lennart Poettering (2013-10-07).
bootup
. systemd manual pages. freedesktop.org. - Jonathan de Boyne Pollard (2018).
init
. nosh Guide. Softwares.
Best Answer
Using the commands
shutdown
andreboot
respectively. To log off from a terminal you can press Ctrl-D or typeexit
.