20 December 2009

How to work with multiple jobs in Bash

An important feature often overlooked by novice console users is job control. It allows some amount of multitasking on a shell by delegating processes to the background and resuming them at a later point. That way, you don't have to launch a second shell or use additional utilities like screen to manage multiple tasks.

Job control is easy. Let's say you have Midnight Commander running. Hit CTRL+Z and you end up back at the shell prompt. You will notice a line above the prompt stating that mc is suspended. The beginning of the line shows mc's job number in square brackets (1 in this case). Ok, let's use that job number to resurrect mc. Input %1 and voĆ­la, mc is back! Similarly, you can use %1 & to unsuspend mc and continue running it as a background job. Note that background jobs are detached from the terminal and will be suspended as soon as they try to read from or write to it.

When working with multiple jobs, you will find the jobs command very useful, which lists the running jobs with their numbers and status.

A more intuitive way to unsuspend a job is to give part of its command line. %mc will unsuspend Midnight Commander, however if you have multiple instances running it will result in an error. Finally, just inputting % will unsuspend the most recently suspended job.

As you can certainly see, this feature is a real time-saver by allowing you to get running applications out of the way and resume them later.

0 Kommentare:

Post a Comment