Wednesday, August 2, 2023
HomeNetworkingTransferring duties from foreground to background and again once more

Transferring duties from foreground to background and again once more


When engaged on the Linux command line, you can begin a process, transfer it to the background, and, while you’re able to reverse the method, deliver it again to the foreground. If you run a command or script within the foreground, it occupies your time on the command line – till it’s completed. When it’s essential to do one thing else whereas nonetheless permitting that first process to finish, you possibly can transfer it to the background the place it would proceed processing and spend your time engaged on one thing else.

The simplest manner to do that is by typing ^z (maintain the Ctrl key and press “z”) after beginning the method. This stops the method. Then sort “bg” to maneuver it to the background. The jobs command will present you that it’s nonetheless operating.

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

Now let’s have a look at a simple manner to do this out.

First, put a easy script like this collectively and make it able to run.

#!/bin/bash

whereas true 
do
  date > x
  sleep 120
carried out

That script will put the present date and time to a file known as “x” each 2 minutes. Because it overwrites the file as soon as it exists, it doesn’t expend further disk house.

Subsequent, begin the script. If the script is named “loop”, you’d use a command like this:

$ loop

Subsequent, use the ^z trick to cease the method after beginning it.

$ loop
^Z
[1]+  Stopped                 loop

Then use the bg command to maneuver it to the background the place it would proceed operating.

$ bg
[1]+ loop &

This explicit script will run ceaselessly or till you kill it, sign off or reboot the system. You’ll be able to terminate it by utilizing the kill command adopted by the background course of id.

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

The jobs command with no output tells you that no extra backgrounded processes are nonetheless operating.

Begin a course of within the background

You can too put a course of within the background while you first begin it by following the command or script title with an & character. Within the instance beneath, I’m operating a second looping script within the background after which utilizing the jobs command to view all backgrounded processes.

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

Discover the and + indicators within the output above output following the [1] and [2] course of numbers. These will solely seem for the 2 most just lately backgounded duties. These jobs will be known as %1, %2 or just as and + while you need to transfer them to the foreground with a command like fg +.

Bringing backgrounded duties again to the foreground

When you’ve got processes which can be operating within the background, you employ the fg command to deliver them again to the foreground. To maneuver the loop process to the foreground, you may use both of the instructions proven beneath.

$ fg %1
$ fg –

You need to use the jobs command to indicate the backgrounded duties.

$ jobs
[2]+  Operating                 loop2 &

Discover that solely one of many two duties remained within the background after the primary was introduced again to the foreground with the fg – command.

Wrap-up

Relying on what you’re engaged on, transferring duties to the background in an effort to take management of the command line for extra urgent duties whereas permitting the backgrounded duties to proceed operating can save time could be a good transfer.

Copyright © 2023 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments