Linux Basics Lesson 22 of 38
Foreground, background, staying on
9 minutes
Learning goals
- Start a job in the background with & and list it with jobs.
- Move a job between foreground and background with Ctrl-Z, bg and fg.
- Keep a job alive after you leave with nohup, and lower its priority with nice and renice.
The shell's own list: &, jobs, fg, bg
& at the end starts a command in the background, and the prompt comes back at once. The number in square brackets is its job number, the second number its PID. jobs shows your list:
sleep 300 &
Purpose: Runs a command in the background, jobs shows the list, fg and bg move it.
Syntax: COMMAND & | jobs | fg [%N] | bg [%N]
Options:
Example:
sleep 300 &
[1] 4243
jobs
[1]+ Running sleep 300 &
jobs -l
[1]+ 4243 Running sleep 300 &sleep here stands for any long-running command. fg brings a job to the foreground, and from there you end it with Ctrl-C or stop it with Ctrl-Z. Ctrl-Z does not end anything: the program stands still, keeping all it has. bg then lets it run on in the background.
Try it yourself in the task terminal below: sleep 300, Ctrl-Z, bg, jobs, fg, Ctrl-C. On a phone, tap Ctrl in the row of extra keys, then the letter.
kill is not loaded on this page, so ending a job works with fg and Ctrl-C here. On a real machine, kill %1 does it from the outside, and the previous lesson explains the signals.
Staying alive: nohup
When you close the terminal or the connection drops, the shell sends its jobs the signal HUP, and most of them end. nohup makes a command ignore that, and sends its output to nohup.out:
nohup sleep 300 &
Purpose: Keeps a command running after the terminal closes, nice and renice set its priority.
Syntax: nohup COMMAND & | nice -n N COMMAND | renice -n N -p PID
Options:
Example:
nohup sleep 300 &
[1] 4243
nohup: ignoring input and appending output to 'nohup.out'
jobs
[1]+ Running nohup sleep 300 &
ls -l nohup.out
-rw------- 1 lerner lerner 0 Oct 17 09:42 nohup.outLater, you look into nohup.out to see how it went.
Priorities: nice and renice
nice starts a command with a lower priority, so that a big batch job does not slow anything else down. renice changes that afterwards. Both take a "friendliness" from -20 to 19, where higher means friendlier to the others. renice reports old and new values, and $! is the PID of the last background job:
nice -n 5 sleep 300 &
[1] 4243
renice -n 10 -p $!
4243 (process ID) old priority 5, new priority 10
As a normal user you may only raise the value, not lower it again. Going back down, and anything below 0, needs sudo on a real server.
Practice
Terminal task 1/2
Start sleep 300 in the background so that your prompt stays free, then show your list of jobs.
Task 1/2: Start sleep 300 in the background so that your prompt stays free, then show your list of jobs.
Start sleep 300 in the background so that your prompt stays free, then show your list of jobs.
Task complete
Solution
sleep 300 &
jobs Why: The list belongs to your shell. Another terminal has its own jobs, and ps is what shows everything on the machine.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/2
Start sleep 300 so that it would survive closing the terminal, in the background.
Task 2/2: Start sleep 300 so that it would survive closing the terminal, in the background.
Start sleep 300 so that it would survive closing the terminal, in the background.
Task complete
Solution
nohup sleep 300 &
jobs Why: nohup alone would block your prompt, and & alone would die with the terminal. It takes both.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
You paused a command with Ctrl-Z. To let it run on in the background, you type:
You paused a command with Ctrl-Z. To let it run on in the background, you type: …
Check works once you have typed an answer.
Predict the output
Command
jobsBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
Check works once you have chosen an answer.
Find the error
This line is meant to keep a job running after you leave. Tap the part that is wrong.
Parts of the command
Check works once you have tapped a part.
Next up: programs that are meant to run all the time.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: sleep 300 &, nohup sleep 300 &
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: done
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
<command> &start in the background -
jobslist of your jobs -
fg / bgto the foreground, on in the background -
nohup <command> &keeps running when you leave
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 19 KB)
- View as image (PNG, 57 KB)