How Will You Be Able To Run Linux Commands In Background?

Linux Commands In Background

Linux is a graphical user interface and a Linux command line is an interface that is based on text to the system. You just need to enter the commands through the keyboard and you will get the feedback in the form of text as well.

This article will let you know the process of running Linux commands in the background. But, before that, you should know the two major problems that take place when a process is associated with a terminal.

First, the controlling terminal is filled with so much output data and error/diagnostic messages.

Second, in the event that the terminal is closed, the process will be terminated along with the baby processes of the main process.

But, that is not something we are dealing with in this part. We will actually look into the matter that deals with Linux run command in background.

Getting into the Process of Running Linux in the Background

In case a process is already in running, then you need to simply press on the ctrl+z keys at the same time from the keyboard in order to stop it.

Then, you need to enter the command ‘bg’. Do this in order to continue with the execution of the commands in the background as a job.

To view all the jobs running in the background by typing ‘jobs’. But, its stdin, stdout, stderr are still joined to the terminal.

$ tar -czf home.tar.gz .

$ bg

$ jobs

Also, you will be able to run the process straight away from the background just by making the use of the ampersand, ‘&’ sign.

$ tar -czf home.tar.gz . &

$ jobs

Here, I have provided an example that will make things clearer to you. The ‘tar’ command was started as a background task though, as error notification was still delivered to the terminal and this means that the procedure is still connected to the controlling terminal.

Here is the command line.

$ tar -czf home.tar.gz . &

$ jobs

How to keep the Linux Commands Running even after Exiting the Terminal

Here, in this part, you are going to use the command called ‘disown’. This command is used after the process has been started off and placed in the background. The work of this particular command is to eliminate a shell job from the activated list jobs of the shell. As a consequence, you will not be utilizing ‘fg’,’bg’ commands on that specific job anymore.

Additionally, during the closing of the controlling terminal, the job will not hang or deliver a SIGHUP to any mini jobs.

Okay, now it is the time we should have a quick look at the example displayed below that uses the ‘disown’ command bash default function.

$ sudo rsync Templates/* /var/www/html/files/ &

$ jobs

$ disown  -h %1

$ jobs

Here is another thing. You will also be able to use the ‘nohup’ command. This actually, also, activates a process in order to continue executing in the background when a user gets out of a shell.

Here is the command line.

$ nohup tar -czf iso.tar.gz Templates/* &

$ jobs

Unfastening a Linux Processes from Controlling Terminal

In order to unfasten or detach a process from a controlling terminal, you have to use the command line format that is mentioned below. This is something that brings more impact on graphical user interface (GUI) applications like Firefox.

Here is the command line format you need to use.

$ firefox </dev/null &>/dev/null &

In Linux, /dev/null is a specific file present in the device that actually gets rid of all data written to it. The command line that is mentioned right above, the input there is read from and the output is delivered to /dev/null file.

Wrapping Up

This was all about running Linux running in the background of your system. The comment section is there to let us know any doubts or how useful it is.

You can share this article with the ones who need this. Do not forget to pay a visit to the other articles that we post regularly on our platform.