There is a semi-secret feature called the “Magic
SysRq Key’ built into the keyboard handler at a level
below the Linux kernel, so it is active even if the kernel is frozen
for some reason. There are several “magic” key commands,
but by far the most often used sequence is to hold down
Alt-SysRq and then slowly press
—upper or lower case doesn’t matter. Pause a second or
two between each letter.
This sequence does a “sane” shutdown and reboot, closing disk buffers and so on, so it is much safer than having to abruptly power down, which can leave the disks in an incorrect state.
If you don’t want to reboot but simply do a safe shutdown, replace the last letter B (= reBoot) with O (= Off) instead. Unfortunately, REISUO doesn’t have a nice mnemonic phrase.
There are several other command letters available. Wikipedia has a complete summary of the how and why; just search for “reisub” or “Magic SysRq”.
From a command line, simply execute the
You will get a display that looks like this, updated every few seconds:
top - 00:48:30 up 4 days, 9:37, 5 users, load average: 0.03, 0.09, 0.13
Tasks: 201 total, 1 running, 200 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.2 us, 0.8 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 6112148 total, 5115908 used, 996240 free, 238724 buffers
KiB Swap: 33042132 total, 3648 used, 33038484 free, 3560372 cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1355 root 20 0 235m 76m 41m S 2.3 1.3 25:16.41 Xorg
2286 dierdorf 20 0 1226m 192m 44m S 1.7 3.2 2:36.48 thunderbird
2468 dierdorf 20 0 1581m 369m 52m S 1.3 6.2 143:05.18 firefox
2180 dierdorf 20 0 2837m 70m 36m S 0.7 1.2 4:34.53 kwin
1 root 20 0 27056 2712 1440 S 0.0 0.0 0:01.12 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:13.74 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/u:0H
8 root rt 0 0 0 0 S 0.0 0.0 0:01.71 migration/0
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
10 root 20 0 0 0 0 S 0.0 0.0 0:16.69 rcu_sched
The default shows the running processes in descending order by current CPU usage. The default column headers are, in order:
There are many control keys to change the display or perform operations. For example:
topis running as root, it can also raise a priority.
M, N, P, T
See the manpage for
top for many more ways to customize
ps -f command to show the sequence of
processes descending from others. You’ll get something like this:
On the current terminal,
UID PID PPID C STIME TTY TIME CMD dierdorf 3623 2754 0 Oct03 pts/4 00:00:00 /usr/bin/zsh dierdorf 19393 3623 0 01:58 pts/4 00:00:00 ps -f
zshis running as PID 3623. The
pscommand is PID 19393, and its parent PID (i.e., the process which started it) is
zsh, as expected. To get the list of all processes and their parents, use the command
ps -Afinstead. Note that
psshows the actual command line used to start a process, which can come in handy. (Hint: use
grepto locate the process of interest:
ps -Af | grep firefoxfor example.)
To kill a process, use the
kill PID command. This
politely asks the process to terminate in an orderly fashion. If it
is stubborn and won’t die, then use
kill -9 PID
instead. This uses brute-force, so the process will not get a chance
to clean up after itself. Note that if you are running
k option prompts for which type of
signal to send.
You can find a PID by using
but you can also kill something by name:
killall -9 firefox, for example. Note that if you
have more than one process with that name running,
killall will, as the name implies, zap all of them.
dfcommand, I get:
Filesystem Type Size Used Avail Use% Mounted on /dev/sda5 ext4 96G 8.4G 83G 10% / /dev/sda6 ext4 96G 5.5G 86G 7% /mint /dev/sda8 ext4 84G 39G 42G 49% /extra dell:/home/dierdorf nfs 79G 15G 61G 20% /dell gw:/home/dierdorf nfs 886G 271G 571G 33% /gw gw:/images nfs 465G 213G 228G 49% /gwimages gw:/extra nfs 788G 565G 183G 76% /gwextra
The last four lines are “foreign” partitions mounted on this laptop.
The problem with this is that NFS is not very smart, particularly when one or more computers have been rebooted. Here’s how to (usually) get things connected again:
(Thesudo umount -lf /gw sudo umount -lf /dell ...etc
-lfoptions force unmount even if the file system doesn’t want to.)
(This is hard to remember, so I alias it tosudo /etc/init.d/nfs-kernel-server restart
sudo mount /gw sudo mount /dell ...
ssh dell nfsrestart exit ssh gw nfsrestart exit
dfcommand will “hang”. Press
Ctrl-Cto get your prompt back.
If the graphical environment is dead, you can usually resurrect it with the following steps:
ps -A | grep Xorg
sudo kill [pid of Xorg] ... or ... sudo kill -9 [pid of Xorg]
startx. If necessary, switch back to the graphics console with
Ctrl-Alt-F7. Note that you’ll have to restart any programs that were previously running in the graphical environment, since they were all children of the original Xorg.
ESCkey. Unfortunately, almost all keyboards have that key in the extreme upper left corner of the keyboard, where it is very difficult for a touch typist to reach it. On the other hand, the almost completely useless
CapsLockkey is right beside the /A/. Therefore I swap the two, placing
ESCwhere I can more easily access it. There are two ways to do keyboard customization like this:
.Xmodmapin your home directory containing lines like this:
The only trickiness is knowing the keycodes for the various keyboard keys. If necessary, use the commandremove Lock = Caps_Lock keycode 0x42 = Escape keycode 0x09 = Caps_Lock add Lock = Caps_Lock pointer = 1 2 3 4 5 7 6 10 8 9 11 12 13
sudo showkey, which will display the key code for each key you press.
.Xmodmapset up, execute the following line:
If it works, add the add the line to yourxmodmap ~/.Xmodmap