Start a task running in the background, so you don't have to wait for it to finish before doing something else. Learn how to use the & operator at the command line.
- [Instructor] Normally, when you run a command in the terminal, that command has full control of the interface. It's center stage, and until it's done, you can't do anything else. You may have run into this when copying a large file or running updates in a terminal window. And you may have opened up another terminal to do something else on the same machine, instead of waiting for your process to finish. There's nothing fundamentally wrong with that, but if we can be more efficient, why wouldn't we? Bash, and some of the other popular shells, like Z shell and K shell, let us run a process in the background.
What does that mean? Think of it as detaching the process from your terminal window, and then telling it to go run over there, giving us back access to the terminal instead of waiting for the process to finish first. Output from the process still gets printed to our screen on the standard output, as long as we don't redirect it. The easiest way to have a task run in the background, is to just start it out running there. We can take a look at this with a quick script that sleeps for a few seconds and then prints some output. We'll take a look at it with cat sleep-then-echo.sh.
If I run this in the terminal with bash sleep-then-echo.sh, I have to wait five whole seconds, during which I can't really interact with the terminal. I could type, but it won't do what I want. And then finally, I get my prompt back. If I type the same command again, but this time follow it up with an ampersand, I'm immediately back at my prompt. The script is running in the background, and after five seconds, I still get my output. It's a little bit strange looking, but the text is printed out to the standard output like usual, even though it looks like I typed a command here at the prompt.
Hitting Enter won't run it or anything like that. You'll notice that when I ran the background command I got some feedback. In this case, one, and another number, 4867. The one is the job number, which will be important in a little bit. And the other number is the process ID. If this were a longer-running task, I'd be able to go find the process with a PS command, or others. When the process is finished, the next time I get a new prompt, I get even more information about the finished process. The job number, a status of Done, and the command that finished running.
This isn't too important right now, but it will be helpful in just a little bit. Using the ampersand with a command is helpful to send it to the background, but it certainly doesn't cover all of the use cases. What if I need to interact with a backgrounded application, for example?
- Starting tasks in the background
- Controlling more than one process
- Managing virtual terminals with the screen utility
- Managing sessions and windows
- Managing virtual terminals with the tmux utility