📅 January 14, 2015
Here is a computer term rarely heard these days: amp off.
To amp off means to run in the background, and it is derived from the ampersand character (& or amp) placed at the end of a command line in a Linux or UNIX terminal.
When a command is executed at a command prompt, the shell waits for completion before returning shell control to the user. This can involve lengthy wait times for time-consuming commands and processes, such as searching for a particular text string that could be located in any of thousands of files.
grep -r "amp off" /
will recursively search for and list all lines and files containing the phrase “amp off” starting from the root directory. This can take a very long time–even with a reasonably fast computer if the hard drive contains a plethora of files. We cannot enter any more commands in the current shell until the process completes. (Press Control + C to abort the process.)
But if we append an ampersand character to the end of the command,
grep -r "amp off" / &
then we can immediately use the shell while the search is performed as a background job. However, entered text will compete with the grep process output in the same terminal unless its output is redirected to a file or another terminal. Let’s see how to redirect grep’s output.
Redirecting Standard Error to a Second Terminal
Suppose there is a second terminal open named /dev/pts/5. (Enter tty within a terminal to find the terminal device. Linux Mint 17.1 is used in this example.) We can redirect standard error from grep to /dev/pts/5 while running grep in the background from the current terminal.
grep -r "amp off" / 2> /dev/pts/5 &
The current terminal will show the job number and process ID (PID) of grep once started.
In this example,  is the job number, and 3769 is the PID.
Any errors will appear in the second terminal /dev/pts/5 to avoid cluttering the current terminal.
To list all jobs running in the current shell, enter jobs. To kill a job, type kill %4, but be sure to replace the 4 with the actual job number. It could be 1, 2, 3, or any other number if multiple jobs are running. kill %1 would terminate job number 1. kill 3769 would also work. Replace 3769 with the actual PID since 3769 is only an example.
Redirecting Both Standard Error and Standard Output
If we want to redirect both standard output and standard error to the second terminal, type:
grep -r "amp off" / &> /dev/pts/5 &
Remember to replace /dev/pts/5 with the destination terminal, which will probably have a different name from this example.
Avoid Ampersand Confusion
Do not confuse the two ampersands:
grep -r "amp off" / &> /dev/pts/5 &
&> and & mean two different things. &> is a shortcut redirection operator that redirects both standard output (stdout) and standard error (stderr) to /dev/pts/5, and & alone at the end of the command line will amp off or run grep in the background so the user can continue using the shell. An ampersand (&) at the end always means “run this command in the background.”
Why Run in the Background?
In the early days of slow computing and full-screen, single-task terminals, the ability to use the shell without waiting for the previous command to complete was a blessing. It eliminated much waiting on a UNIX system.
In these days of sleek GUIs and faster computers that allow multiple virtual terminals, shell-based background jobs and redirection is more suited for command line parlor tricks that impress the uninitiated rather than as an essential piece of computer usage.
But who knows? There are times when we might be limited to a full-screen shell until we can restore the GUI, so it might be worthwhile to know how to amp off a time-consuming command while we perform other tasks.