UNIX / Linux Tutorial
1.12.4 Stopping And Restarting Jobs | 1.13 Using The vi Editor | 1.13.1 Concepts
1.13.2 Starting vi
1.12.4 Stopping And Restarting Jobs There is another way to put a job into the background. You can start the job normally (in the foreground), stop the job, and then restart it in the background. First, start the yes process in the foreground, as you did before: / home / patrick # > yes > / dev / null Again, because yes is running in the foreground, you shouldn't get the shell prompt back. Now, rather than interrupt the job with Ctrl-C , suspend the job. Suspending a job doesn't kill it: it only temporarily stops the job until you restart it. To do this, press the suspend key, which is usually Ctrl-Z . / home / patrick # yes > > / dev / null ctrl-Z [1] + Stopped yes > / dev / null / home / patrick # While the job is suspended, it's simply not running. CPU time isn't used for the job. However, you can restart the job, which causes the job to run again as if nothing ever happened. It will continue to run where it left off. To restart the job in the foreground, use the fg command (for "foreground"). /home/patrick# fg yes > / dev / null The shell displays the name of the command again so you're aware of which job you just put into the foreground. Stop the job again with Ctrl-Z . This time, use the bg command to put the job into the background. This causes the command to run just as if you started the command with "&" as in the last section. / home / patrick # bg [1] + yes > / dev / null & / home / patrick # And you have your prompt back. Jobs should report that yes is indeed running, and you can kill the job with kill as we did before. How can you stop the job again? Using Ctrl-Z won't work, because the job is in the background. The answer is to put the job in the foreground with fg, and then stop it. As it turns out, you can use fg on either stopped jobs or jobs in the background. There is a big difference between a job in the background and a job that is stopped. A stopped job is not running—it's not using any CPU time, and it's not doing any work (the job still occupies system memory, although it may have been swapped out to disk). A job in the background is running and using memory, as well as completing some task while you do other work. However, a job in the background may try to display text on your terminal, which can be annoying if you're trying to work on something else. For example, if you used the command: / home / patrick # yes & without redirecting stdout to /dev/null, a stream of y's would be displayed on your screen, without any way for you to interrupt it. (You can't use Ctrl-C to interrupt jobs in the background.) In order to stop the endless y's, use the fg command to bring the job to the foreground, and then use Ctrl-C to kill it. Another note. The fg and bg commands normally affect the job that was last stopped (indicated by a "+" next to the job number when you use the jobs command). If you are running multiple jobs at once, you can put jobs in the foreground or background by giving the job ID as an argument to fg or bg, as in: / home / patrick # fg %2 (to put job number 2 into the foreground), or / home / patrick # bg %3 (to put job number 3 into the background). You can't use process ID numbers with fg or bg. Furthermore, using the job number alone, as in: / home / patrick # %2 is equivalent to: / home / patrick # fg %2 Just remember that using job control is a feature of the shell. The fg, bg and jobs commands are internal to the shell. If for some reason you use a shell that doesn't support job control, don't expect to find these commands available. In addition, there are some aspects of job control that differ between bash and tcsh. In fact, some shells don't provide job control at all—however, most shells available for Linux do. 1.13 Using The vi Editor A text editor is a program used to edit files that are composed of text: a letter, C program, or a system configuration file. While there are many such editors available for Linux, the only editor that you are guaranteed to find on any UNIX or Linux system is vi— the "visual editor." vi is not the easiest editor to use, nor is it very self-explanatory. However, because vi is so common in the UNIX/Linux world, and sometimes necessary, it deserves discussion here. Your choice of an editor is mostly a question of personal taste and style. Many users prefer the baroque, self-explanatory and powerful emacs—an editor with more features than any other single program in the UNIX world. For example, Emacs has its own built-in dialect of the LISP programming language, and has many extensions (one of which is an Eliza-like artificial intelligence program). However, because Emacs and its support files are relatively large, it may not be installed on some systems. vi, on the other hand, is small and powerful but more difficult to use. However, once you know your way around vi, it's actually very easy. This section presents an introduction to vi. We will not discuss all of its features, only the ones you need to know to get started. You can refer to the man page for vi if you're interested in learning more about this editor's features. Alternatively, you can read the book. Learning the vi Editor from O'Reilly and Associates, or the VI Tutorial from Specialized Systems Consultants (SSC) Inc. 1.13.1 Concepts While using vi, at any one time you are in one of three modes of operation. These modes are called command mode, insert mode and last line mode. When you start up vi, you are in command mode. This mode lets you use commands to edit files or change to other modes. For example, typing "x" while in command mode deletes the character underneath the cursor. The arrow keys move the cursor around the file you're editing. Generally, the commands used in command mode are one or two characters long. You actually insert or edit text within insert mode. When using vi, you'll probably spend most of your time in this mode. You start insert mode by using a command such as "i" (for "insert") from command mode. While in insert mode, you can insert text into the document at the current cursor location. To end insert mode and return to command mode, press Esc . Last line mode is a special mode used to give certain extended commands to vi. While typing these commands, they appear on the last line of the screen (hence the name). For example, when you type ":" in command mode, you jump into last line mode and can use commands like "wq" (to write the file and quit vi), or "q!" (to quit vi without saving changes). Last line mode is generally used for vi commands that are longer than one character. In last line mode, you enter a single-line command and press Enter to execute it. 1.13.2 Starting vi The best way to understand these concepts is to fire up vi and edit a file. The example "screens" below show only a few lines of text, as if the screen were only six lines high instead of twenty-four. The syntax for vi is: vi filename where filename is the name of the file to edit. Start up vi by typing: / home / patrick # vi test to edit the file test. You should see something like: ˜ ˜ ˜ ˜ ˜ ˜ "test" [New file] This column of "˜" characters indicates you are at the end of the file. The represents the cursor.
1.1 Introduction 1.2.10 Referring To Home Directories 1.3.4 Copying Files
1.6 Exploring The File System 1.8 Wildcards 1.9.3 Pipes 1.10.3 Permissions Dependencies
1.12.4 Stopping And Restarting Jobs 1.13.3 Inserting Text 1.13.9 Including Other Files
1.14.3 Shell Initialization Scripts System Administration 2.3.1 The /etc/imitate file
2.4 Managing File Systems 2.6 Managing Users 2.6.5 Groups 2.7.2 gzip and compress
2.8.3 Making Backups To Tape Devices 2.9.1 Upgrading The Kernel
2.9.3 Installing A Device Driver Module
BOOK: LINUX QUICK COMMAND REFERENCE
http://personal.atl.bellsouth.net/~psadler
© copyright KnowledgeWorks, Inc. (2001)