Moving tasks from foreground to background and back again


When working on the Linux command line, you can start a task, move it to the background, and, when you’re ready to reverse the process, bring it back to the foreground. When you run a command or script in the foreground, it occupies your time on the command line – until it’s finished. When you need to do something else while still allowing that first task to complete, you can move it to the background where it will continue processing and spend your time working on something else.

The easiest way to do this is by typing ^z (hold the Ctrl key and press “z”) after starting the process. This stops the process. Then type “bg” to move it to the background. The jobs command will show you that it is still running.

$ mytask
^Z
[2]+  Stopped                 mytask
$ bg
$ jobs
[1]+  Running                 mytask &

Now let’s look at an easy way to try this out.

First, put a simple script like this together and make it ready to run.

#!/bin/bash

while true 
do
  date > x
  sleep 120
done

That script will put the current date and time to a file called “x” every 2 minutes. Since it overwrites the file once it exists, it doesn’t use up additional disk space.

Next, start the script. If the script is called “loop”, you would use a command like this:

$ loop

Next, use the ^z trick to stop the process after starting it.

$ loop
^Z
[1]+  Stopped                 loop

Then use the bg command to move it to the background where it will continue running.

$ bg
[1]+ loop &

This particular script will run forever or until you kill it, log out or reboot the system. You can terminate it by using the kill command followed by the background process id.

$ kill %1
[1]+  Terminated              loop
$ jobs

The jobs command with no output tells you that no more backgrounded processes are still running.

Start a process in the background

You can also put a process in the background when you first start it by following the command or script name with an & character. In the example below, I’m running a second looping script in the background and then using the jobs command to view all backgrounded processes.

$ loop2 &
[2] 4651
$ jobs
[1]-  Running                 loop &
[2]+  Running                 loop2 &

Notice the and + signs in the output above output following the [1] and [2] process numbers. These will only appear for the two most recently backgounded tasks. These jobs can be referred to as %1, %2 or simply as and + when you want to move them to the foreground with a command like fg +.

Bringing backgrounded tasks back to the foreground

If you have processes that are running in the background, you use the fg command to bring them back to the foreground. To move the loop task to the foreground, you could use either of the commands shown below.

$ fg %1
$ fg –

You can use the jobs command to show the backgrounded tasks.

$ jobs
[2]+  Running                 loop2 &

Notice that only one of the two tasks remained in the background after the first was brought back to the foreground with the fg – command.

Wrap-up

Depending on what you’re working on, moving tasks to the background so that you can take control of the command line for more pressing tasks while allowing the backgrounded tasks to continue running can save time can be a smart move.

Copyright © 2023 IDG Communications, Inc.



Source link