- Understand why this is useful
- Create background tasks in bash
- View currently running background tasks
- Switch between background tasks
Sometimes you might only have access to a single terminal window (for example, with the Learn IDE!) and you'll want to run multiple processes. Luckily, there is an easy solution for this: Job control in bash!
This will be really helpful in the OO Student Scraper lab where you will want to run a server so you can access a locally stored website and run tests that will need to access this server.
If you would like a start a new process (like a local server) in the background, all you need to do as add an &
to the end of the command like this: jekyll serve &
. This process will now run in the background in this terminal window.
Note: You may have to press
enter
orreturn
to get your prompt back.
Great, now we've got some tasks quietly running in the background, so how to we keep track of them? Simple, use the command jobs
in your terminal to get a list of all jobs running in this window. Each job will have a job number in front of it that will look like this: [1]
. Now we can easily keep track of all processes that are currently running.
You can easily switch which job runs in the foreground with the fg
command followed by the job number. So if our jekyll server is running in the background as job number 1, we can simply switch to it by doing fg 1
. Once you've switched your process to running in the foreground you can act on it as normal (for example, you could now shut your server down with ctrl + c
).