AI Hints
Home›Linux Tutorial›Processes
Linux · Lesson 81 of 120

Background Jobs

& and jobs.

⏱ 2 min read▶ Video at 1:47:46🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Some commands take a long time. A big download, or a backup. While they run, your terminal is busy, and you can't type anything else. Unless you send them to the background.

Background Jobs
From the video: Background Jobs
  • Some commands take ages
  • The terminal is stuck
  • Send them to the background

The ampersand

Put an ampersand at the end of a command, and it runs in the background. You get the prompt back straight away. In your own terminal, the shell also prints a job number in brackets, and the PID. Jobs lists what's running in the background, from this terminal.

Terminal
sleep 300 &
jobs
[1]+  Running                 sleep 300 &

Moving jobs around

If a command is already running in the front, press control Z. That pauses it, and puts it in the background, stopped. Then BG lets it carry on in the background. FG brings a job back to the front. And control C stops whatever's in the front.

cmd &start in the background
Ctrl + Zpause the front job
bg / fgto background / to front
jobslist your jobs

Background jobs and logging out

Background jobs belong to your terminal. If you close it, they usually stop too. To keep something running after you log out, start it with no hup in front. For serious long running programs, Linux has services, which we'll see soon.

keyvalue
Close the terminaljobs usually stop
nohup cmd &keep running
Servicesfor long running programs
jobs, fg, bgmanage them

Summary

An ampersand runs a command in the background, jobs lists them, and FG and BG move them around. Next, stopping processes with kill.

Key takeaways
  • cmd &
  • jobs
  • fg, bg, Ctrl+Z
Next up: kill: Stopping Processes — signals