25th May 2022

Sending Process to Background in Linux Terminal(Ctrl+Z and fg)

While performing your operations with commands in the terminal, you wanted to make corrections in a file (resolv.conf etc.) with the nano editor. In this case, you type sudo nano document name on the command line and enter it into the editor. When you enter the editor, the command line is now gone. What should you do if you want to do something on the command line even though you are not done with the file you are working on?

  • I open a new tab.
  • Save the document with Ctrl + O, exit with Ctrl + X. I type the command in the terminal, run it and then open the nano editor again.
  • Ctrl + Z key combination and fg command

The first of these options might be if you are working with a visual desktop. However, this may not be possible on the remote server you connect to with ssh. The second option seems to be a method from the first ages when the computer was invented. You will find that there is a short and quick way to fix this problem.

Ctrl+Z key combination and fg command

Developers who are bored with such save-exit-re-enter processes have developed the Ctrl + Z key combination and the fg command. In this way, it is possible to send the working editor to the background without exiting and switch back to the command line. You can even open another document in the editor from the command line and send it to the background. After you send two open documents to the background, you can open another document and send it to the background.

LEARN MORE  Adding Cisco Router / Switch IOS Image to GNS3 on Ubuntu

Creating files

First, let’s create test1.txt, test2.txt and test3.txt files.

Creating files-echo command
Creating files-echo command

 

Ctrl+z keys

We open the documents with the nano editor and send them to the background with the Ctrl + z keys. In this way, we reach the command line again without closing the document.

As can be seen below, with each Ctrl + Z key combination, a number is given next to the transaction sent to the background. This number is used for you to recall the background task in question.

LEARN MORE  What is the Difference Between apt-get update/upgrade/dist-upgrade and How is it Used?

fg command

Now let’s recall a task we sent to the background. We use the fg command for this. Anyway, in the screen outputs, shell tells us to use fg to return to nano.

If you just type fg on the command line, you will return to the last closed action. However, if you want to go back to the number 1 process, it will be enough to give the fg 1 command.

fg 2
fg 2

fg 2 stoped
fg 2 stoped

 

Note1: If you want to see all the processes running in the background, you can use the jobs command.

Note2: Although the subject is explained through nano editor in this article, it is possible to refer to the same background with vi or other editors.

 

Leave a Reply

Your email address will not be published. Required fields are marked *