That's an excellent question! Backgrounding a process, like we did with python3 -m http.server 8080 &, offers several benefits, especially in a command-line environment:
-
Reclaiming your terminal: The primary benefit is that it frees up your terminal. When you run a command without
&, your shell "waits" for that command to complete before giving you the prompt back. If the command runs continuously (like our web server), your terminal would be tied up, and you wouldn't be able to execute any other commands in that session. By backgrounding it, you can immediately continue using the same terminal window for other tasks. -
Running multiple tasks concurrently: You can have multiple processes running in the background simultaneously. For example, you might background a script that processes data, while still using the same terminal to browse directories or edit files.
-
Non-interactive processes: It's ideal for tasks that don't require further interaction from you once they've started. Our web server just sits there and listens; it doesn't need input or continuous monitoring from the terminal.
-
Keeping processes alive (with
nohuporscreen/tmux): While&alone starts a background process, if you close the terminal window, the background process typically terminates. To keep processes running even after you close your terminal, you'd usually combine&withnohup(e.g.,nohup python3 -m http.server 8080 &) or use terminal multiplexers likescreenortmux. In this lab, for simplicity,&is sufficient as you're expected to keep the terminal open for subsequent steps.
In our current lab, backgrounding the Python web server allows you to immediately proceed to the next steps, like verifying the server and running Nmap scans, all within the same terminal session without needing to open a new one.
Does this explanation help clarify the advantages of backgrounding?