Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 67 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
67
Dung lượng
451,86 KB
Nội dung
CHAPTER 26 ■ TAKING CONTROL OF THE SYSTEM 510 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 log out and log in again, or reboot. 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 typically used with the aux command options (there’s no need to provide a dash before the options, as with most commands): 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 <PID number> For example, to kill a process with a PID of 5122, you would type this: kill 5122 If, after that, 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, which is the chief process of the Firefox web browser, you would use the following command: killall firefox ■ Caution Make sure you’re as specific as possible when using the killall command. Issuing a command like killall bin will kill all processes that might have the word bin in their name! CHAPTER 26 ■ TAKING CONTROL OF THE SYSTEM 511 CLEARING UP CRASHES Sometimes a crashed process can cause all kinds of problems. The shell you’re working at may stop working, or the GUI itself might stop working properly. In cases like this, it’s important to remember that you can have more than one instance of the command- line shell up and running at any one time. For example, if a process crashes and locks up GNOME Terminal, simply start a new instance of GNOME Terminal (Applications Accessories Terminal). Then use top within the new window to kill the process that is causing trouble for the other terminal window. If the crashed program affects the entire GUI, you can switch to a virtual console by pressing Ctrl+Alt+F1. Although the GUI disappears, you will not have killed it, and no programs will stop running. Instead, you’ve simply moved the GUI to the background while a shell console takes over the screen. Then you can use the virtual console to run top and attempt to kill the process that is causing all the problems. When you’re ready, you can switch back to the GUI by pressing Ctrl+Alt+F7. If you know the name of the program that’s crashed, a quick way of getting rid of it is to use the pgrep command. This searches the list of processes for the program name you specify and then outputs the PID number. So if, say, Nautilus had frozen, you could type pgrep nautilus. Then you would use the kill command with the PID number that’s returned. Controlling Jobs Whenever you start a program at the shell, it’s assigned a job number. Jobs are quite separate from processes and are designed primarily for users to understand what programs are currently doing on the system. You can see which jobs are running at any one time by typing the following at the shell prompt: jobs When you run a program, it usually takes over the shell in some way and stops you from doing anything until it’s finished what it’s doing. However, it doesn’t have to be this way. Adding an ampersand symbol (&) after the command will cause it to run in the background. This is not much use for commands that require user input, such as vim or top, but it can be handy for commands that churn away until they’re completed. For example, suppose that you want to decompress a large Zip file. For this, you can use the unzip command. As with Windows, decompressing large Zip files can take a lot of time, during which time the shell would effectively be unusable. However, you can type the following to retain use of the shell: unzip myfile.zip & When you do this, you’ll see something similar to the following, although the four-digit number will be different: [1] 7483 i CHAPTER 26 ■ TAKING CONTROL OF THE SYSTEM 512 This tells you that unzip is running in the background and has been given job number 1. It also has been given process number 7483 (although bear in mind that when some programs start, they instantly kick off other processes and terminate the one they’re currently running, so this won’t necessarily be accurate). ■ Tip If you’ve ever tried to run a GUI program from the shell, you might have realized that the shell is inaccessible while it’s running. After you quit the GUI program, the control of the shell is returned to you. By specifying that the program should run in the background with the & (ampersand symbol), you can run the GUI program and still be able to type away and run other commands. You can send several jobs to the background, and each one will be given a different job number. In this case, when you want to switch to a running job, you can type its number. For example, the following command will switch you to the background job assigned the number 3: %3 You can exit a job that is currently running by pressing Ctrl+Z. It will still be there in the background, but it won’t be running (officially, it’s said to be sleeping). To restart it, you can switch back to it, as just described. Alternatively, you can restart it but still keep it in the background. For example, to restart job 2 in the background, leaving the shell prompt free for you to enter other commands, type the following: %2 & You can bring the command in the background into the foreground by typing the following: fg When a background job has finished, something like the following will appear at the shell: [1]+ Done unzip myfile.zip Using jobs within the shell can be a good way of managing your workload. For example, you can move programs into the background temporarily while you get on with something else. If you’re editing a file in vim, you can press Ctrl+Z to stop the program. It will remain in the background, and you’ll be returned to the shell, where you can type other commands. You can then resume vim later on by typing fg or typing % followed by its job number. ■ Tip Also useful is Ctrl+C, which will kill a job that’s currently running. For example, if you previously started the unzip command in the foreground, pressing Ctrl+C will immediately terminate it. Ctrl+C is useful if you accidentally start commands that take an unexpectedly long time to complete. CHAPTER 26 ■ TAKING CONTROL OF THE SYSTEM 513 NOHUP What if you want to start a command running in a terminal window, but then want to close that terminal window? As soon as you close the window, any processes started within it are also closed. Try this now— type gcalctool at the prompt to start the Calculator application and then quit the terminal window. This happens because, when you quit, the parent process sends any process that it started a hang-up signal. Some processes are designed to ignore the hang-up signal, so in the preceding example not every process will quit when the terminal window does, but most will. As you might expect, the hang-up signal is a remnant of the way UNIX used to work many years ago, when people dialed into computers across slow connections; it is designed to stop processes from continuing to consume resources after the user has hung up the phone and thereby ended the session! To get around processes quitting like this, you can use the nohup command. This stands for no hang-up, and in simple terms, it tells the command you specify to stick around, even after the process that started it has ended (technically, the command is told to ignore the SIGHUP signal). However, commands run via nohup can still be killed in the usual way. To use nohup, simply add it before the command, for example: nohup unzip myfile.zip If the command requires sudo or gksu powers, add either of these after the nohup command. Any command output (including error messages) is sent to the file nohup.out, which you can then view in a text editor. Note that if you run a command via nohup using sudo or gksu, the nohup.out file will have root privileges. If that’s the case, you will also have to delete the nohup.out file via sudo before you can use nohup again as an ordinary user—because otherwise, nohup will be unable to overwrite the root- owned nohup.out. Summary This chapter has covered taking complete control of your system. You looked at what processes are, how they’re separate from programs, and how they can be controlled or viewed by using programs such as top and ps. In addition, you explored job management under BASH. You saw that you can stop, start, and pause programs at your convenience. In the next chapter, we take a look at several tricks and techniques that you can use with the BASH shell to finely hone your command-line skills. Download from Wow! eBook <www.wowebook.com> PART 6 ■ ■ ■ Appendixes A P P E N D I X A ■ ■ ■ 517 Introducing the BASH Shell As you learn in Chapter 1, strictly speaking, the word Linux refers to just the kernel, which is the fundamental, invisible program that runs your PC and lets everything happen. However, on its own, the kernel is completely useless. It needs programs to let users interact with the PC and do cool stuff, and it needs a lot of system files (also referred to as libraries) to provide vital functions. The GNU Project provides many of these low-level pieces of code and programs. This is why many people refer to the Linux OS as GNU/Linux, acknowledging that without the GNU components Linux wouldn’t have gotten off the starting blocks. The GNU Project provides various shell programs too. A shell is what the user interacts with on a day-to-day basis, whether by mouse or keyboard. The word originates from the fact that the shell is the outer layer of the OS, which encapsulates the kernel (and in some instances protects it by filtering out bad user commands). Some shells offer graphical functionality but, in general, the word shell is understood to mean text-only interfaces. These text shell programs are also known as terminal programs, and they’re often colloquially referred to as command-line prompts, in reference to the most important component they provide. This kind of shell lets you take control of your system in a quick and efficient way. Although using the shell is not strictly necessary nowadays, because almost everything can be done in Ubuntu using the graphical interface, it remains true that by using the shell you become the true master of your own system. This appendix introduces the BASH shell, which is the default shell on Ubuntu systems. What Is the BASH Shell? The best way of explaining the BASH shell to a Windows user is to compare it to the DOS command prompt. It lets you issue commands directly to the OS via the keyboard without needing to mess around with the mouse and windows (although it is sometimes possible to use the mouse within a BASH shell to copy and paste text, and sometimes to control simple text-based menus). The big difference is that the BASH shell has commands for just about everything you might do on your system, whereas the DOS command prompt is mostly limited to tools capable of manipulating and viewing files and directories. In the old days, the DOS command prompt was also the visible layer of an entire operating system in which DOS programs were designed to be run. However, the shell is merely one of the many ways of accessing the Linux kernel and subsystems. It’s true that many programs are designed to run via the BASH shell, but technically speaking, most actually run on the Linux OS, and simply take input and show their output via the BASH shell. The instinctive response of a longtime Windows user is to be wary of the BASH shell, because it presents an entirely new way of working and a new set of concepts to learn. There’s no denying that the shell provides plenty of challenges for the newbie user, but the rewards it brings—both in terms of sense APPENDIX A ■ INTRODUCING THE BASH SHELL 518 of achievement, as well as making users more effective at controlling their computers—more than outweigh the initial difficulties. Linux finds itself with the BASH shell largely because Linux is a clone of UNIX. In the early days of UNIX, the text-based shell was the only way for users to control the computer. Typing in commands directly is one of the most fundamental ways of controlling any type of computer and, in the evolutionary scale, comes straight after needing to set switches and watch blinking lights in order to run programs. That the BASH shell can trace its history back to the early days of UNIX might sound like a tacit indication that the BASH is somehow primitive—far from it. It’s one of the most efficient and immediate ways of working with your computer. Many people consider the command-line shell to be a fast, efficient way of using a computer that has yet to be superseded by a better method. ■ Note When you run a shell on a Linux system, the system refers to it as a tty device. This stands for teletypewriter, a direct reference to the old system of inputting data on what were effectively electronic typewriters connected to mainframe computers. These, in turn, took their names from the devices used to automate the sending and receiving of telegrams in the early part of the 20th century. Most Linux distributions come with a choice of different shell programs. However, the default shell for most Linux systems is BASH, as is the case with Ubuntu. BASH stands for Bourne Again SHell. The name is a pun and alludes to the origins of Bash as a rewrite of the Bourne shell, a tried-and-tested program from the heyday of UNIX in the late 1970s. The other shells available include PDKSH (Public Domain Korn SHell, based on Korn Shell, another early UNIX shell) and ZSH (Z SHell), a more recent addition. These are usually used by people who want to program Linux in various ways or by those who simply aren’t happy with BASH. ■ Note Discussing the technical differentiators between shells is beyond the scope of this book, but you’ll find an excellent comparison at Wikipedia: http://en.wikipedia.org/wiki/Comparison_of_computer_shells. The BASH shell is considered by many to be the best of all worlds in that it’s easy enough for beginners to learn, yet is able to grow with them and offer additional capabilities as necessary. BASH is capable of scripting, for example, which means you can even create your own simple programs. Why Bother with the Shell? You might have followed the instructions in this book and consider yourself an expert in Linux. But the real measure of a Linux user comes from your abilities at the shell. Most modern Linux distributions prefer you to use the GUI to do nearly everything. To this end, they provide GUI tools for just about every task you might want to undertake. Ubuntu is strong in this regard, and you can configure a lot of things from the Desktop (as this book helps to prove). However, it’s well worth developing at least some command-line shell skills, for a number of reasons: [...]... simply have to use the shell for a particular task It’s consistent among distributions: All Linux systems have shells and understand the same commands (broadly speaking) However, not all Linux systems have Ubuntu s graphical configuration programs SUSE Linux uses its own GUI configuration tool, as does Mandriva Linux Therefore, if you ever need to use another system or decide to switch distributions,... we see: ubuntu@ ubuntu-desktop:~$ ■ Note The first part is the username—the user account we created during installation and use to log in to the PC After the @ sign is the hostname of the PC, which we also chose when installing Ubuntu The hostname of the PC isn’t important on most home systems, but assumes relevance if the PC is part of a network The @ sign tells us that we are running user ubuntu on... it when you first installed Ubuntu and is stored in a system variable ■ Note A variable is the method Linux uses to remember things such as names, directory paths, or other data Many system variables are vital for the running of Ubuntu These variables can be seen by typing set at the command prompt The information about where your programs are stored and therefore where Ubuntu should look for commands... running user ubuntu on the computer with the hostname ubuntu- desktop After the colon is the current directory you’re browsing In this example, the tilde symbol (~) appears instead of an actual path or directory name This is merely Linux shorthand for the user’s /home directory In other words, wherever we see a ~ on our test PC, we read it as /home /ubuntu/ After this is the dollar symbol ($), which indicates... shell As you learn in Chapter 20, unlike with Windows, installing a program on Ubuntu doesn’t necessarily mean the program automatically appears on the Applications menu In fact, unless the installation routine is specifically made for the version of Linux you’re running, this is unlikely ■ Note Unlike with DOS programs, Ubuntu programs that describe themselves as command-line are rarely designed to... example, to create an alias that lets you type cls instead of clear, type this: alias cls='clear' Note that the Ubuntu command must appear in single quotation marks Also note that the dir command is already implemented under Ubuntu as a separate command that functions almost identically to the Linux ls command, although it’s intended for only brief file listings In most cases, it’s far better just to... discussed earlier that changes the network card’s IP address: ifconfig eth0 192 .168.0.15 up If you’ve never used the shell before, it might as well be Sanskrit written on the side of an ancient tomb What on Earth does ifconfig mean? And why is the word up at the end? ■ Note If you’re curious, the command tells the network card, called by Linux eth0, to adopt the specified IP address The word up at the end... start working now If the word down were there instead, it would deactivate! Don’t worry about understanding all this right now; later in this appendix, we explain how you can learn about every Linux command 5 19 APPENDIX A ■ INTRODUCING THE BASH SHELL Learning to use the shell requires learning terms like these Hundreds of commands are available, but you really need to learn only about 10 or 20 for everyday... test PC, we read it as /home /ubuntu/ After this is the dollar symbol ($), which indicates being currently logged in as an ordinary user, as opposed to the root user However, unlike most other Linux distributions, Ubuntu doesn’t use the root account during day-to-day operations, so this is a moot point Finally, there is a cursor, and this is where you can start typing commands! ■ Note If you were to log... possible When you become familiar with it, you’ll see that it is a beautiful concept The shell is simple, elegant, and powerful When Should You Use the Shell? The amount of use the Linux shell sees is highly dependent on the user Some Linux buffs couldn’t manage without it They use it to read and compose e-mail, and even to browse the Web (usually using Mailutils and the Lynx program, respectively) However, . distributions: All Linux systems have shells and understand the same commands (broadly speaking). However, not all Linux systems have Ubuntu s graphical configuration programs. SUSE Linux uses its. the 20th century. Most Linux distributions come with a choice of different shell programs. However, the default shell for most Linux systems is BASH, as is the case with Ubuntu. BASH stands for. in this book and consider yourself an expert in Linux. But the real measure of a Linux user comes from your abilities at the shell. Most modern Linux distributions prefer you to use the GUI to