Linux Tips and Tricks

Reboot Even If System Utterly Broken

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 R…E…I…S…U…B —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”.

Who Is Stealing My CPU and Memory?

From a command line, simply execute the top command. 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

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:

toggles showing idle tasks —those which have used no CPU since the last screen update.
sets the update delay in seconds.
(re-nice) allows you to lower the priority of a process. If top is running as root, it can also raise a priority.
kills a task.
M, N, P, T
sort by %MEM, PID, %CPU, or TIME+ columns, respectively.

See the manpage for top for many more ways to customize the output.

Who Started What?

Use the ps -f command to show the sequence of processes descending from others. You’ll get something like this:

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
On the current terminal, zsh is running as PID 3623. The ps command 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 -Af instead. Note that ps shows the actual command line used to start a process, which can come in handy. (Hint: use grep to locate the process of interest: ps -Af | grep firefox for example.)

Die, Die, Die!

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 top, the k option prompts for which type of signal to send.

You can find a PID by using top or ps, but you can also kill something by name: killall firefox or 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.

Restore NFS Sanity

NFS (Network File System) is the normal way for Linux-to-Linux communication. For example, on this computer, if I execute the df command, 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:

  1. Unmount each of the nfs-connected partitions:
    sudo umount -lf /gw
    sudo umount -lf /dell
    (The -lf options force unmount even if the file system doesn’t want to.)
  2. Re-synchronize the NFS server:
    sudo /etc/init.d/nfs-kernel-server restart
    (This is hard to remember, so I alias it to nfsrestart.)
  3. Issue mount commands for the stuff you previously unmounted:
    sudo mount /gw
    sudo mount /dell
  4. You have about a 50% chance that this will work. If it doesn’t, you will also have to issue the command on the other computers:
    ssh dell
    ssh gw
  5. Note that if things aren’t correct, the df command will “hang”. Press Ctrl-C to get your prompt back.

What Happened to the GUI?

If the graphical environment is dead, you can usually resurrect it with the following steps:

  1. Switch to a full-screen console with Ctrl-Alt-F1.
  2. Log in with your usual ID and password to get a command prompt.
  3. Issue the following command to see if Xwindows is running at all:
    ps -A | grep Xorg
  4. If you see it, kill it with the command
    sudo kill [pid of Xorg]
    ... or ...
    sudo kill -9 [pid of Xorg]
  5. Restart the graphical environment with the command 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.

Swapping Keys

I like both the Emacs and VI editors, and both of them make frequent use of the ESC key. 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 CapsLock key is right beside the /A/. Therefore I swap the two, placing ESC where I can more easily access it. There are two ways to do keyboard customization like this:
  1. If it is a common modification (e.g.the ESC-CapsLock swap) and if you are running KDE, simply go to System Settings-->Input Devices-->Keyboard-->Advanced. Double-click the “Ctrl key position” button and choose the layout you want.
  2. If you’re trying to do something non-standard, or if you’re not using KDE, then you have to do it the hard way:
    1. Create a file called .Xmodmap in your home directory containing lines like this:
      remove 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
      The only trickiness is knowing the keycodes for the various keyboard keys. If necessary, use the command sudo showkey, which will display the key code for each key you press.
    2. Once you have .Xmodmap set up, execute the following line:
      xmodmap ~/.Xmodmap
      If it works, add the add the line to your .bashrc or .zshrc file.

Last modified: Thu Oct 10 06:12:26 CDT 2013