terminal.cab

Linux Basics Lesson 22 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Foreground, background, staying on

9 minutes

Learning goals

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:

  • jobs -l also shows the PIDs

Example:

sleep 300 &
[1] 4243
jobs
[1]+  Running                 sleep 300 &
jobs -l
[1]+  4243 Running                 sleep 300 &

in the glossary: sleep

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:

  • -n 10 friendliness: higher means lower priority

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.out

in the glossary: nohup

Later, 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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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

jobs

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
What does jobs print?

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

  1. Question 1 of 3What is the difference between Ctrl-C and Ctrl-Z?

    Check works once you have chosen an answer.

  2. Question 2 of 3Which signal does nohup protect a command from?

    Check works once you have chosen an answer.

  3. Question 3 of 3A batch job should get in nobody's way. Which value do you pass to nice?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 23, Services with systemctl (9 min)

What you can do now: sleep 300 &, nohup sleep 300 &

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: done
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 23, Services with systemctl (9 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.