Выбрать главу

The next step is to kill the process ID of the yes command, so you need to understand what each of the important fields means:

► PID — The process ID

► User — The owner of the process

► PR — Priority

► NI — Niceness

► Virt — Virtual image size in kilobytes

► Res — Resident size in kilobytes

► Shr — Shared memory size in kilobytes

► S — Status

► %CPU — CPU use

► %Mem — Memory use

► Time+ — CPU time

► Command — The command being run

Several of these fields are unimportant unless you have a specific problem. The ones we are interested in are PID, User, Niceness, %CPU, %MEM, Time+, and Command. The Niceness of a process is how much time the CPU allocates to it compared to everything else on the system: 19 is the lowest, and -19 is the highest.

With the columns explained, you should be able to find the process ID of the errant yes command launched earlier; it is usually the first number below PID. Now type k, enter that process ID, and press Enter. You are prompted for a signal number (the manner in which you want the process killed), with 15 provided as the default. Signal 15 (also known as SIGTERM, for "terminate") is a polite way of asking a process to shut down, and all processes that are not wildly out of control should respond to it. Give top a few seconds to update itself, and the yes command should be gone. If not, you need to be more forcefuclass="underline" type k again, enter the PID, and press Enter. When prompted for a signal to send, enter 9 and press Enter to send SIGKILL, which means "terminate whether you like it or not."

You can choose the fields to display by pressing f. A new screen appears that lists all possible fields, along with the letter you need to press to toggle their visibility. Selected fields are marked with an asterisk and have their letter, for example:

* A: PID = Process Id

If you press the a key, the screen changes to this:

a: PID = Process Id

When you have made your selections, press Enter to return to the normal top view with your normal column selection.

You can also press F to select the field you want to use for sorting. This works in the same way as the field selection screen, except that you can select only one field at a time. Again, press Enter to get back to top after you have made your selection, and the output will be updated with the new sorting.

If you press B, text bolding is enabled. By default, this bolds some of the header bar and any programs that are currently running (as opposed to sleeping), but if you press x you can also enable bolding of the sorted column. You can use y to toggle bolding of running processes.

The last command to try is r, which enables you to re-nice — or adjust the nice value — of a process. You need to enter the PID of the process, press Enter, and enter a new nice value. Keep in mind that 19 is the lowest and -20 is the highest; anything less than 0 is considered "high" and should be used sparingly.

Printing the Location of a Command with which

The purpose of which is to tell you the exact command that would be executed if you typed it. For example, which mkdir returns /bin/mkdir, telling you that running the command mkdir runs /bin/mkdir.

Combining Commands

So far, we have been using commands only individually, and for the large part that is what you will be doing in practice. However, some of the real power of these commands lies in the capability to join them to get exactly what you want. There are some extra little commands we have not yet covered that are often used as glue because they do one simple thing that enables a more complex process to work.

All the commands we have examined have printed their information to the screen, but this is often flexible. There are two ways to control where output should go: piping and output redirection. A pipe is a connector between one command's output and another's input. Rather than send its output to your terminal, a command sends that output directly to another command for input. Output redirection works in a similar way to pipes but is usually used for files. We look at pipes first and then output redirection.

Two of the commands covered so far are ps and grep: the process lister and the string matcher. You can combine the two to find out which users are playing Nethack right now:

$ ps aux | grep nethack

That creates a list of all the processes running right now and sends that list to the grep command, which filters out all lines that do not contain the word nethack. Fedora allows you to pipe as many commands as you can sanely string together. For example, you could add in the wc command, which counts the numbers of lines, words, and characters in its input, to count precisely how many times Nethack is being run:

$ ps aux | grep nethack | wc -l

The -l (lowercase L) parameter to wc prints only the line count.

Using pipes in this way is often preferable to using the -exec parameter to find, simply because many people consider find to be a black art and so anything that uses it less frequently is better! This is where the xargs command comes in: It converts output from one command into arguments for another.

For a good example, consider this mammoth find command from earlier:

$ find / -name "*.txt" -size +10k -user paul -not -perm +o=r -exec chmod o+r {} \;

That command searches every directory from / onward for files matching *.txt that are greater than 10KB, are owned by user paul, and do not have read permission for others. Then it executes chmod on each of the files. It is a complex command, and people who are not familiar with the workings of find might have problems understanding it. So, what we can do is break up the single command into two parts: a call to find and a call to xargs. The conversion would look like this:

$ find / -name "*.txt" -size +10k -user paul -not -perm +o=r | xargs chmod o+r

That has eliminated the confusing {} \; from the end of the command, but it does the same thing, and faster, too. The speed difference between the two exists because using -exec with find causes it to execute chmod once for each file. However, chmod accepts many files at a time and, because the same parameter is used each time, you should take advantage of that. The second command, using xargs, is called once with all the output from find, and so saves many command calls. The xargs command automatically places the input at the end of the line, so the previous command might look something like this: