Archive for November, 2007

CHAPTER 16 TAKING CONTROL (Web hosting unlimited bandwidth) OF THE SYSTEM

Friday, November 23rd, 2007

CHAPTER 16 TAKING CONTROL OF THE SYSTEM Note No magic is involved in killing processes. All that happens is that top sends them a terminate signal. In other words, it contacts them and asks them to terminate. By default, all processes are designed to listen for commands such as this; it s part and parcel of how programs work under Linux. When a program is described as crashed, it means that the user is unable to use the program itself to issue the terminate command (such as Quit). A crashed program might not be taking input, but its processes will probably still be running. In many cases, zombie processes simply won t go away. When this happens, you have two options. The first is to restart the program that is likely to be the zombie s owner, in the hope that it will reattach with the zombie, and then quit the program. With any luck, it will take the zombie child with it this time. Alternatively, you can simply reboot your PC. But it s important to note that zombie processes are harmless and can be left in peace on your system! Using Other Commands to Control Processes You don t always need to use top to control processes. A range of quick and cheerful shell commands can diagnose and treat process problems. The first of these is the ps command. This stands for Process Status and will report a list of currently running processes on your system. This command is normally used with the -aux options: ps aux This will return a list something like what you see when you run top. If you can spot the problematic process, look for its PID and issue the following command: kill For example, to kill a process with a PID of 5122, you would type this: kill 5122 If, after this, you find the process isn t killed, then you should use the top program, as described in the previous sections, because it allows for a more in-depth investigation. Another handy process-killing command lets you use the actual process name. The killall command is handy if you already know from past experience what a program s process is called. For example, to kill the process called firefox-bin, which is the chief process of the Firefox web browser, you would use the following command: killall firefox-bin Caution Make sure you re as specific as possible when using the killall command. Issuing a command like killallbin will kill all processes that might have the word bin in them!

Web site layout - 262 CHAPTER 16 TAKING CONTROL OF THE

Thursday, November 22nd, 2007

262 CHAPTER 16 TAKING CONTROL OF THE SYSTEM With any luck, the process (and the program in question) will disappear. If it doesn t, the process you ve killed might be the child of another process that also must be killed. To track down the parent process, you need to configure topto add the PPID field, for the parent process ID, to its display. To add this field, type f, and then b. Press Enter to return to the process list. The PPID column will appear next to the process name on the right of the window. It simply shows the PID of the parent process. You can use this information to look for the parent process within the main list of processes. The trick here is to make sure that the parent process isn t something that s vital to the running of the system. If it isn t, you can safely kill it. This should have the result of killing the child process you uncovered prior to this. Caution If the PPID field in top displays a value of 1, that means the process doesn t have a parent process. In both the PPID and PID fields, you should always watch out for low numbers, particularly one-, two- or three-digit numbers. These are usually processes that started early on when Linux booted and that are essential to the system. Controlling Zombie Processes Zombie processes are those that are children of processes that have terminated. However, for some reason, they failed to take their child processes with them. Zombie processes are rare on most Linux systems. Despite their name, zombie processes are harmless. They re not actually running and don t take up system resources. However, if you want your system to be spick-and-span, you can attempt to kill them. In the top-right area of top, you can see a display that shows how many zombie processes are running on your system, as shown in Figure 16-3. Zombie processes are easily identified because they have a Z in the status (S) column within top s process list. To kill a zombie process, type k, and then type its PID. Then type 9, rather than accept the default signal of 15. Figure 16-3. You can see at a glance how many zombie processes are on your system by looking at the top right of top s display.

Free web servers - CHAPTER 16 TAKING CONTROL OF THE SYSTEM

Thursday, November 22nd, 2007

CHAPTER 16 TAKING CONTROL OF THE SYSTEM it created in order to run. The main process is called the parent, and the ones it creates are referred to as child processes. Tip You can see a nice graphical display of which parent owns which child process by typing pstree at the command-line shell. While this should mean your system runs smoothly, badly behaved programs sometimes don t go away. They stick around in the process list. Alternatively, you might find that a program crashes and so isn t able to terminate itself. In very rare cases, some programs that appear otherwise healthy might get carried away and start consuming a lot of system resources. You can tell when this happens because your system will start slowing down for no reason, as less and less memory and/or CPU time is available to run actual programs. In all of these cases, the user usually must kill the process in order to terminate it manually. This is easily done using top. The first task is to track down the crashed or otherwise problematic process. In top, look for a process that matches the name of the program, as shown in Figure 16-2. For example, the Firefox web browser generally runs as a process called firefox-bin. Figure 16-2. You can normally identify a program by its name in the process list. Caution You should be absolutely sure that you know the correct process before killing it. If you get it wrong, you could cause other programs to stop running. Because top doesn t show every single process on its screen, tracking down the trouble- causing process can be difficult. A handy tip is to make top show only the processes created by the user you re logged in under. This will remove the background processes started by root. You can do this within top by typing u, and then entering your username. Once you ve spotted the crashed process, make a note of its PID number, which will be at the very left of its entry in the list. Then type k. You ll be asked to enter the PID number. Enter that number, and then press Enter once again (this will accept the default signal value of 15, which will tell the program to terminate).

260 CHAPTER 16 TAKING CONTROL OF (Disney web site) THE

Wednesday, November 21st, 2007

260 CHAPTER 16 TAKING CONTROL OF THE SYSTEM It s possible to alter the ordering of the process list according to other criteria. For example, you can list the processes by the quantity of memory they re using, by typing M while top is up and running. You can switch back to CPU ordering by typing P. RENICING A PROCESS You can set how much CPU time a process receives while it s actually running. This is done by renicing the process. This isn t something you should do on a regular basis, but it can prove very handy if you start a program that then uses a lot of system resources and makes the system unbearably slow. The first thing to do is to use top to spot the process that needs to be restrained and find out its PID number. This will be listed on the left of the program s entry on the list. Once you know this, type r, and then type in the PID number. You ll then be asked to specify a renice value. The scale goes from 20, which is considered the highest priority, to 19, which is considered the lowest. Therefore, you should type 19. After this, you should find some responsiveness has returned to the system, although how much (if any) depends on the nature of the programs you re running. You might be tempted to bump up the priority of a process to make it run faster, but this may not work because of complexities in the Linux kernel. In fact, it might cause serious problems. Therefore, you should renice with care and only when you must. Controlling Processes Despite the fact that processes running on your computer are usually hidden away, Linux offers complete, unrestricted, and unapologetic control over them. You can terminate processes, change their properties, and learn every item of information there is to know about them. This provides ample scope for damaging the currently running system but, in spite of this, even standard users have complete control over processes that they personally started (one exception is zombie processes, described a bit later in this section). As you might expect, the root user (or any user who adopts superuser powers) has control over all processes that were created by ordinary users, as well as those processes started by the system itself. The user is given this degree of control over processes in order to enact repairs when something goes wrong, such as when a program crashes and won t terminate cleanly. It s impossible for standard users to damage the currently running system by undertaking such work, although they can cause themselves a number of problems. Note This control over processes is what makes Linux so reliable. Because any user can delve into the workings of the kernel and terminate individual processes, crashed programs can be cleaned up with negligible impact on the rest of the system. Killing Processes Whenever you quit a program or, in some cases, when it completes the task you ve asked of it, it will terminate itself. This means ending its own process and also that of any other processes

CHAPTER 16 TAKING CONTROL OF THE (Free web hosts) SYSTEM

Tuesday, November 20th, 2007

CHAPTER 16 TAKING CONTROL OF THE SYSTEM 259 Table 16-1. The top Program Process Information Column Description NI This column shows the nice value of the process. This refers to how charitable a process is in its desire for CPU time. A high figure here (up to 19) indicates that the process is willing to be interrupted for the sake of other processes. A negative value means the opposite: the process is more aggressive than others in its desire for CPU time. Some programs need to operate in this way, and this is not necessarily a bad thing. VIRT This column shows the amount of virtual memory used by the process.1 RES This column shows the total amount of physical memory used.1 SHR This column shows the amount of shared memory used. This refers to memory that contains code that is relied on by other processes and programs. S This column shows the current status of the task. Generally, the status will either be sleeping, in which case an S will appear, or running, in which case an R will appear. Most processes will be sleeping, even ones that appear to be active. Don t worry about this; it just reflects the way the Linux kernel works. A Z in this column indicates a zombie process (a child of a process that has been terminated). %CPU This column shows the CPU use, expressed as a percentage.2 %MEM This column shows the memory use, again expressed as a percentage.2 TIME+ This column shows a measure of how long the process has been up and running. COMMAND This shows the actual name of the process itself. 1 Both VIRT and RES are measured in kilobytes unless an m appears alongside the number; in which case, you should read the figure as megabytes. 2 The %CPU and %MEM entries tell you in easy-to-understand terms how much of the system resources a process is taking up. This list will probably be longer than the screen has space to display, so top orders the list of processes by the amount of CPU time the processes are using. Every few seconds, it updates the list. You can test this quite easily. Let your PC rest for a few seconds, without touching the mouse or typing. Then move the mouse around for a few seconds. You ll see that the process called Xorg leaps to the top of the list (or appears very near the top). Xorg is the program that provides the graphical subsystem for Linux, and making the mouse cursor appear to move around the screen requires CPU time. When nothing else is going on, moving the mouse causes Xorg to appear as the number one user of CPU time on your system. Tip Typing d while top is running lets you alter the update interval, which is the time between screen updates. The default is three seconds, but you can reduce that to one second or even less if you wish. However, a constantly updating topprogram starts to consume system resources and can therefore skew the diagnostic results you re investigating. Because of this, a longer, rather than shorter, interval is preferable.

258 CHAPTER 16 TAKING CONTROL OF THE (Web hosting packages)

Tuesday, November 20th, 2007

258 CHAPTER 16 TAKING CONTROL OF THE SYSTEM Figure 16-1. The top program gives you an eagle-eye view of the processes running on your system. Here s an example of a line taken from top on my test PC, shown with the column headings from the process list: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 5499 root 15 0 78052 25m 60m S 2.3 5.0 6:11.72 Xorg A lot of information is presented here, as described in Table 16-1. Table 16-1. The top Program Process Information Column Description PID The first number is the process ID (PID). This is the unique number that the system uses to track the process. The PID comes in handy if you want to kill (terminate) the process (as explained in the next section of this chapter). USER This column lists the owner of the particular process. As with files, all processes must have an owner. A lot of processes will appear to be owned by the root user. Some of them are system processes that need to access the system hardware, which is something only the root user is allowed to do. Other processes are owned by root for protection; root ownership means that ordinary users cannot tamper with these processes. PR This column shows the priority of the process. This is a dynamic number, showing where the particular process is in the CPU queue at the present time.

Net web server - CHAPTER 16 Taking Control of

Monday, November 19th, 2007

CHAPTER 16 Taking Control of the System By now, you should be starting to realize that the shell offers an enormous amount of power when it comes to administering your PC. The BASH shell commands give you quick and efficient control over most aspects of your Linux setup. However, the shell truly excels in one area: controlling the processes on your system. Controlling processes is essential for administration of your system. You can tidy up crashed programs, for example, or even alter the priority of a program so that it runs with a little more consideration for other programs. Unlike with Windows, this degree of control is not considered out of bounds. This is just one more example of how Linux provides complete access to its inner workings and puts you in control. Without further ado, let s take a look at what can be done. Viewing Processes A process is something that exists entirely behind the scenes. When the user runs a program, one or many processes might be started, but they re usually invisible unless the user specifically chooses to manipulate them. You might say that programs exist in the world of the user, but processes belong in the world of the system. Processes can be started not only by the user, but also by the system itself to undertake tasks such as system maintenance, or even to provide basic functionality, such as the GUI system. Many processes are started when the computer boots up, and then they sit in the background, waiting until they re needed (such as programs that send mail). Other processes are designed to work periodically to accomplish certain tasks, such as ensuring system files are up- to-date. You can see what processes are currently running on your computer by running the top program. Running top is simply a matter of typing the command at the shell prompt. As you can see in Figure 16-1, top provides very comprehensive information and can be a bit overwhelming at first sight. However, the main area of interest is the list of processes (which top refers to as tasks).

CHAPTER 15 WORKING WITH TEXT FILES Summary (Web site management)

Sunday, November 18th, 2007

CHAPTER 15 WORKING WITH TEXT FILES Summary In this chapter, we examined how text files can be manipulated. In many ways, the BASH shell is built around manipulating text, and we explored various tools created with this goal in mind. We started with the commands that can display text files (or part of them). We then looked at how the vi text editor can be used to both edit and create documents. Next, we explored how regexes can be used with the grep command to create sophisticated search strings, which can uncover any text within documents. Finally, you saw how to compare text files. In the next chapter, we ll look at how you can use various command-line tools to take control of your system.

Apache web server for windows - 254 CHAPTER 15 WORKING WITH TEXT FILES

Saturday, November 17th, 2007

254 CHAPTER 15 WORKING WITH TEXT FILES grep is very powerful. It can be complicated to master, but it offers a lot of scope for performing extremely precise searches that ensure you find only what you re seeking. It s well worth reading through its man pages. You can also refer to books on the subject, of which there are many. A good example is Regular Expression Recipes: A Problem-Solution Approach, by Nathan A. Good (1-59059-441-X; Apress, 2004). Comparing Text Files If you want to compare the differences between two text files, one way to do this is to use the diff command. This is designed primarily to uncover small changes in otherwise identical documents, such as revisions made by another person. Of course, it can also be used to prove that two files are identical. If you run the files through diff, and it shows no output, it has been unable to spot any differences. diff is ordinarily used like this: diff mytextfile1 mytextfile2 If diff spots any differences between the files, the results are a little more complicated than you might be used to. Any lines that are different within the files will appear on screen. Those lines that are identical won t be displayed. Lines preceded with a left angle bracket (<) are from the first file, while those with a right angle bracket (>) are from the second file. For a different display, you could type something like this: diff y mytextfile1 mytextfile2 This places the two lists side-by-side and highlights lines that are different with a pipe symbol (|). However, it requires a lot more screen space than using diff without the -y option. Note When you use the -y command option with diff, it will struggle to fit the output in a standard GNOME Terminal window. If it is maximized on a 17-inch screen (1024 768 resolution), it should be just large enough to fit the information in, depending on the complexity of the files being compared. By specifying the -a command option, you can make diff process binary files, too. This is a handy way of comparing virtually any kind of files, including program files, to see if they re identical. If there s no output from diff, then the two files are identical. If your screen fills with gibberish, then the files are clearly different. Incidentally, if you want to compare three documents, you can use a very similar command: diff3. Check the command s man page to learn more about how it works.

CHAPTER 15 (Space web hosting) WORKING WITH TEXT FILES then

Saturday, November 17th, 2007

CHAPTER 15 WORKING WITH TEXT FILES then told it to search for myfile_2draft.doc, and so on. Notice that the example has a backslash before the period separating the file extension from the filename. This indicates to grep that it should interpret the period as an element of the string to be searched for, rather than as a wildcard character, which is how grep usually interprets periods. You don t need to specify a range of characters in this way. You can simply enter whatever selection of characters you want to substitute into the search string. Here s an example: grep ‘myfile[12345].doc’ * This will attempt to find any mention of myfile1.doc, myfile2.doc, myfile3.doc, and so on, in any file within the directory. Here s another example: grep ‘[GgNn]ome’ * This will let you search for the word Gnome within files but takes into account any possible misspelling of the word by people who forget to use the silent G, and any use of uppercase or lowercase. This is only scratching the surface of what regexes can do. For example, many regexes can be combined together into one long search string, which can provide astonishing accuracy when searching. Table 15-3 contains some simple examples that should give you an idea of the power and flexibility of regexes. Table 15-3. Some Examples of Regular Expressions Search String Description ‘document[a-z]’ Returns any lines containing the string document followed by any single letter from the range a through z. ‘document[A-Za-z]’ Returns any lines containing the string document followed by the letters A through Z or a through z. Note that no comma or other character is needed to separate possibilities within square brackets. ‘document.’ Returns any lines containing the string document followed by any other character. The period is used as a wildcard signifying any single character. ‘document[[:digit:]]’ Returns any lines containing the string document followed by any number. ‘document[[:alpha:]]’ Returns any lines containing the string document followed by any character. ‘^document’ Returns any lines that have the string document at the beginning. The caret symbol (^) tells grep to look only at the beginning of each line. ‘document$’ Returns any line that has the string document at the end of the line. The dollar sign symbol ($) tells grep to look for the string only at the end of lines. ‘document[^1-6]’ Returns lines that have the string document in them but not if it s followed by the numbers 1 through 6. When used in square brackets, the caret character (^) produces a nonmatching list a list of results that don t contain the string.