Linux on call · Chapter 20 of 30
Jobs and the background
By the end of this chapter you can run slow commands in the background, move them between foreground and background, and start a program that keeps running after your terminal closes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Processes and the system
- 4 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
Kabir needs two things (they are in note.txt). Today's menu sync is slow, and while it runs your prompt is gone. And the kitchen's ticket printer must run all day: Bunty started it from his terminal, closed his laptop, and the printer stopped with him. The kitchen cooked from memory for an hour.
So far every command you ran held the terminal until it finished, and lived exactly as long as the terminal did. You could open a second terminal for each slow job, and keep a laptop open all day for the printer, and still lose it all the moment the connection drops.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Run the menu sync in the background and check it with jobs
Run the slow menu sync in the background, so you can keep typing while it runs.
1. In the chapter folder, run bin/tapri-menu-sync.sh with & at the end. (Or start it, press Ctrl+Z, then run bg.)
2. While it runs, run jobs. It shows the sync as Running.
3. Wait a few seconds until it is done. Run wait, or run jobs again until it says Done.
4. Run cat menu-synced.txt. The sync makes this file when it finishes.
To pass: you started the sync with & (or used bg) and you ran jobs, in the chapter folder, and menu-synced.txt is in the chapter folder. A command with a typing mistake or an error does not count, so type it again.
- Task 2
Start one ticket printer that keeps running when the terminal closes
Start the ticket printer so that it keeps running after you close the terminal.
1. Start bin/tapri-printer.sh with nohup in front and & at the end.
2. Run pgrep -af tapri-printer. It lists every printer that is running.
3. If there is more than one printer, stop the extra ones with kill and their PIDs.
To pass: exactly one ticket printer is running when you press Check, and it was started with nohup. (A printer started with setsid, or a job you disown in the chapter folder, also counts.)
- Task 3
Send the printer's tickets into logs/printer.log
Send the printer's tickets into a log file, not to nohup.out or the screen.
1. When you start the printer, send its output (stream 1) into logs/printer.log with >.
2. Send its errors (stream 2) into the same file. Put 2>&1 after the first redirection.
3. If a printer is already running with the wrong output, stop it. Then start it again the right way.
4. Run tail logs/printer.log. You see tickets.
To pass: exactly one printer is running, and its output goes into logs/printer.log in the chapter folder.
- Task 4 · interview question
Interview: the signal ignored with nohup
Interview question: which signal does nohup make a program ignore?
1. Find the signal a terminal's jobs get when the terminal closes. Its number is 1.
2. Run kill -l. It lists every signal with its number.
3. Write the name of signal 1 into answer.txt, in the chapter folder. A sentence around it is fine.
To pass: answer.txt names the signal that nohup makes a program ignore.
What you will learn
- & and jobs: run a command in the background and keep typing.
- Ctrl+Z, bg and fg: pause a job, and move it back and forth.
- nohup: start a program that survives the terminal closing, with its output in a file you choose.
More in Processes and the system
Build "Jobs and the background" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.