Keeping Your Shell Programs Under Control


In upcoming chapters we're going to be running fairly involved applications at the console, and you'll be using editors like vi and emacs to work on documents and write shell scripts (small shell programs). Before we reach that point, it's important that you know about shell-based job control.

You can log in to several virtual consoles simultaneously to run more than one full-screen application at a timefor example, an emacs on console one and a vi on console two. Switching back and forth between virtual consoles, however, can be both confusing and clumsy. And you may have noticed that even if you start emacs from a Terminal window, you can't actually use the command line in that terminal window until after you exit emacs once again. More to the point, after you learn to log in to Linux systems remotely via telnet or ssh, you need to be able to use the command line without the luxury of multiple virtual consoles.

To maximize your ability to use the command line even while other applications are running these types of situations, you can use shell-based control techniques to pause and resume multiple tasks or applications, like vi or emacs, from a single command prompt. By using these techniques, you can switch between running applications, move jobs to the foreground or background, and end tasks that are no longer needed. The following sections teach you how to use these and other techniques to comfortably multitask at the Linux console.

Moving Between Multiple Open Applications

Assume for illustrative purposes that you need to be able to start and then switch back and forth between emacs and vi without losing your place in either application. Perhaps you are working on an article comparing the two, or perhaps you are simply editing a file in vi but taking breaks every hour or so to play the dunnet game mentioned earlier when you learned about emacs.

You can use the Ctrl+Z keystroke in a virtual console or in the Terminal window to suspend work on an open application so that you can open another application. You can use the jobs command to list all open applications.

Take a moment to load the myvifile.txt file created earlier in this chapter back into the vi editor:

 [you@workstation20 ~]$ vi myvifile.txt 

When the vi editor appears on the screen, the command prompt is hidden, unavailable to you. You can now begin to edit the file, making changes as necessary. But what happens when it's time to take a dunnet break?

You can suspend the vi process by pressing Ctrl+Z:

 [1]+ Stopped        vim myvifile.txt [you@workstation20 ~]$ 

The vi process has now been suspended. Note the number 1 in brackets; this is the job number of the interrupted vi process. Feel free to start emacs and enter M-x dunnet inside the application to load the dunnet game if you want.

You can play the game for a while, but eventually you must return to your document in vi because you can't play forever. However, to do so, you don't have to exit emacs and lose your place in the game. You can suspend the emacs process in the same way you suspended the vi process earlierby pressing Ctrl+Z:

 [2]+ Stopped        emacs [you@workstation20 ~]$ 

You now have two text editors in your job list that are suspended. To see a list of your current jobs at any time, enter the jobs command:

 [you@workstation20 ~]$ jobs [1]- Stopped        vim myvifile.txt [2]+ Stopped        emacs [you@workstation20 ~]$ 

Jobs can remain suspended indefinitely without harm; you can continue to work with the shell to perform other tasks, and vi and emacs will be ready to pick up exactly where they left off.

Quitting a Running Job

You can sometimes use Ctrl+C to simply quit a running job if you don't want merely to suspend it. However, Ctrl+C doesn't always work. For example, it doesn't work in emacs or vi, although it does work during the output of a particularly lengthy locate job.


Resuming a Job with fg

To return to your editing work in vi, you can use the fg command, passing the percent sign (%) and the correct job number as arguments:

 [you@workstation20 ~]$ fg %1 

As was reported by the jobs command, the vi process is shell job number 1; after you enter the fg (foreground) command supplying %1 as an argument, vi returns your editing display to exactly the same state it was in when you suspended it.

You can repeat this suspend and resume process as needed, suspending and resuming applications in any combination or order.

Running a Job in the Background with bg

Some commands in Linux can take a long time to run, especially on older systems. At such times, the shell can provide an ideal solution. Commands that do not require user intervention to finish can be run as background tasks. You can use the bg command to run jobs in the background. This command then frees you up to return to work in other open applications.

Again for purposes of illustration, assume that while you are editing with vi and playing with emacs, you decide that you also need to generate a list of all the files in your Linux file system that locate can tell you about. To save a list of this sort to a file called fileslist.txt, you must suspend whatever editor you are working in by using the Ctrl+Z keystroke and enter a command to create such a list for you:

 [you@workstation20 ~]$ locate '*' > fileslist.txt 

The locate command is given a pattern that will match every file in its database. The output has been redirected to the file fileslist.txt. Simple enough in concept, but this job will take quite awhile to finish on most PCs. Let's pause this job and then make it a background task. Press Ctrl+Z to interrupt the command in progress:

 [3]+ Stopped        locate '*' >fileslist.txt [you@workstation20 ~]$ 

Notice that the command is assigned a job number of 3 and the command prompt has returned. The command is now suspended; unless it is at some point resumed, it will never finish. To resume it in the background, use the bg (background) command:

 [you@workstation20 ~]$ bg %3 [3]+ locate '*' > fileslist.txt & [you@workstation20 ~]$ 

The command is now running in the background; Linux will continue to work on the task until it is complete. In the meantime, you are free to return to your editor or to your dunnet game by using the fg command. To see the updated list of jobs first, use the jobs command:

 [you@workstation20 ~]$ jobs [1]- Stopped         vim myvifile.txt [2]+ Stopped         emacs [3]  Running         locate '*' >fileslist.txt & [you@workstation20 ~]$ 

As you continue to work with the shell, you will at some point receive a message that your background process has finished:

 [3]  Done          locate '*' >fileslist.txt & 

After a process has finished, it will no longer appear in the jobs list along with the other running or suspended jobs.

Killing and Starting Background Jobs

There are two more commands related to job control that you can use to feel at home on the command line. The first is kill. You can use the kill command with a job number to forcibly terminate a job that you don't want any longer:

 [you@workstation20 ~]$ kill %2 [2]+ Stopped         emacs [1]- Stopped         vim myvifile.txt [2]+ Terminated      emacs [you@workstation20 ~]$ 

Although kill aids in destroying jobs, the ampersand (&) aids in creating them. Instead of using Ctrl+Z and then the bg command, if you want to start a job that can run in the background from the beginning, simply follow it on the command line with an ampersand. For example, earlier you could have used

 [you@workstation20 ~]$ locate '*' > fileslist.txt & [2] 10413 [you@workstation20 ~]$ 

The first number returned after starting a job in the background is the job number, with which you are now familiar. The second number is the system process number; you learn more about using the system process table in Chapter 28, "Command-Line System Administration."



    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net