Background job in linux

Running Linux Commands in Background and Foreground

Learn how to run commands in background in Linux. You’ll also learn how to bring the background jobs back to foreground.

If you have a long-running task, it’s not always wise to wait for it to finish. I mean why keep the terminal occupied for a particular command? In Linux, you can send a command or process to the background so that the command would be running but the terminal will be free for you to run other commands.

In this tutorial, I’ll show you a couple of ways to send a process in the background. I’ll also show you how to bring the background processes back to the foreground.

Start a Linux process in the background directly

If you know that the command or process is going to take a long time, it would be a better idea to start the command in the background itself.

To run a Linux command in the background, all you have to do is to add an ampersand (&) at the end of the command, like this:

Let’s take a simple bash sleep command and send it to the background.

When the command finishes in the background, you should see information about that on the terminal.

Send a running Linux process to the background

If you already ran a program and then realized that you should have run it in the background, don’t worry. You can send a running process to the background as well.

What you have to do here is to use Ctrl+Z to suspend the running process and then use ‘bg‘ (short for background) to send the process in the background. The suspended process will now run in the background.

Let’s take the same example as before.

[email protected]:~$ sleep 60 ^Z [1]+ Stopped sleep 60 [email protected]:~$ bg [1]+ sleep 60 &

See all processes running in the background

Now that you know how to send the processes in the background, you might be interested in knowing which commands are running in the background.

For this purpose, you can enter this command in the terminal:

Let’s put some commands in the background first.

Now the jobs command will show you all the running jobs/processes/commands in the background like this:

jobs [1] Running firefox & [2]- Running gedit & [3]+ Stopped vim

Do you notice the numbers [1], [2] and [3] etc? These are the job ids. You would also notice the – and + sign on two of the commands. The + sign indicates the last job you have run or foregrounded. The – sign indicates the second last job that you ran or foregrounded.

Читайте также:  Dpkg linux установка пакета

Bring a Process to Foreground in Linux

Alright! So you learned to run commands in the background in Linux. But what about bringing a process running in the background to the foreground again?

To send the command to the background, you used ‘bg’. To bring the background process back, use the command ‘fg’.

Now if you simply use fg, it will bring the last process in the background job queue to the foreground. In our previous example, running ‘fg’ will bring Vim editor back to the terminal.

If you want to bring a certain process to the foreground, you need to specify its job id. The job id is the number you see at the beginning of each line in the output of the ‘jobs’ command.

Where n is the job id as displayed in the output of the command jobs.

This was a quick one but enough for you to learn a few things about running commands in the background in Linux. I would advise learning nohup command as well. This command lets you run commands in the background even after you log out of the session.

If you have questions or suggestions, please leave a comment below.

Источник

How to Foreground a Background Process in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

As we know, a program in execution generally takes an input, processes it, and gives us the appropriate output. Linux, as a multitasking operating system, supports the execution of many processes – basically, programs or commands – as background and foreground jobs.

In this tutorial, we’ll discuss interactive processes and non-interactive processes. Also, we’ll learn what job control is in Linux. We’ll also discuss basic ways to multitask from the command line — something that’s necessary when running time-intensive programs.

2. Interaction and Job Control

A terminal session initializes and controls interactive processes. In other words, there has to be someone to connect to the system to start these processes; they do not start automatically. These processes can run in the foreground, thereby occupying the terminal that starts the program. We can’t start other applications as long as this process is running in the foreground.

Alternatively, they can run in the background, so that the terminal in which we start the program can accept new commands while the program is running. Such kinds of processes are known as non-interactive processes. While a process runs in the background, the user can do other things in the terminal.

Читайте также:  Kali linux использование gpu

The shell offers a feature called job control that allows easy handling of multiple processes. This mechanism switches processes among the background and foreground jobs. Using this system, programs can also start in the background immediately.

Job control is the ability to move processes or commands between the foreground and background and to suspend and resume their execution. To implement this, bash introduces the concept of a job, which is essentially a command that one or more processes execute.

The shell uses a few simple data structures in its job control implementation. There is a structure to represent a child process, including its process ID, its state, and the status it returns.

3. Background and Foreground Jobs

A process that connects to the terminal is called a foreground job. A job is said to be in the foreground because it can communicate with the user via the screen and the keyboard.

On the other hand, a process that disconnects from the terminal and cannot communicate with the user is called a background job. If the background job requires interaction with the user, it will stop and wait until establishing a connection to the terminal.

We can place the jobs that do not require interaction from the user as they run (like sorting a large file) in the background. This allows the user to access the terminal and continue to work, instead of waiting for a long job to finish:

background foreground jobs

3.1. Starting a Job in the Background

We can use the jobs command to list all the jobs that the current shell has invoked. The shell lists the job ID of each job, along with the status (running, stopped, or otherwise), and the command that started the job. The shell considers the most recent job to be the current job, marked with a plus sign.

The commands related to job control will apply to the current job. All other jobs, marked with a minus sign, are previous jobs. Jobs can be referred to by job ID by using the percent sign. Thus, job 1 is referred to as %1, job 2 is %2, and so forth.

To place a job in the background, we can simply add the ampersand character (&) at the end of a shell command.

To sort a file called “foo”, and place the results in a file called “bar”, we can issue the command:

3.2. Suspending a Foreground Job

We can (usually) tell Linux to suspend a job that is currently connected to the terminal by typing Ctrl-Z. The shell will inform us that the process has been suspended, and it will assign the suspended job a job ID.

There’s a big difference between a suspended job and a job running in the background. When a suspended job is stopped, no processing takes place for that job until it gets run, either in the foreground or in the background.

Читайте также:  Linux process monitoring and restart

3.3. Placing a Foreground Job into the Background

If we start a job in the foreground and would like to place it in the background, the first thing to be done is to suspend the job with a Ctrl-Z, which frees the terminal:

$ tail -f temp.log ^Z[1]+ Stopped tail -f temp.log

Then, we can issue the command bg to place the suspended job in the background:

$ bg [1] tail -f temp.log $ jobs [1]+ Running tail -f temp.log

The bg command can accept a job ID as an argument. If no job ID is given, bg assumes we are referring to the current (suspended) job.

3.4. Bringing a Background Job to the Foreground

We can reconnect a background job to our terminal with the Linux command fg.

The fg command will accept a job ID as an argument. Make sure to include the percent sign:

This command will bring job 2 into the foreground. If no job ID is given, fg will assume we’re referring to the current (suspended) job.

3.5. Starting a Suspended Job

If we have a suspended job that we’d like to resume running, we must first decide whether we want it to run in the foreground or the background. Once we’ve found the job ID of the suspended job with the jobs command, we can then use bg (to run the job in the background) or fg (to run the job in the foreground).

4. Signals

Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. Common uses of signals are to suspend, terminate, or kill a process.

We can suspend the execution of a background process with Ctrl-Z, which sends the signal SIGTSTP:

$ tail -f temp.log ^Z[1]+ Stopped tail -f temp.log $ jobs [1]+ Stopped tail -f temp.log

We can terminate a background process by sending it to the foreground and then terminating it with Ctrl-C, which sends the signal SIGINT.

Of course, we can use these Linux controls to stop or terminate the execution of a process. But, a more direct approach to terminating a background process is to use the kill command. It doesn’t use job IDs — instead, it uses PIDs. The kill -9 command will terminate the process with the corresponding :

$ jobs [1]+ Running tail -f temp.log $ kill -9 97 [1]+ Killed tail -f temp.log

By default, the kill command sends the kill signal, SIGKILL, an immediate termination.

Similar to Ctrl-Z, we can also use kill to send a stop signal, SIGSTOP, which is a process suspend signal:

$ jobs [1]+ Running tail -f temp.log $ kill -19 152 [1]+ Stopped (signal) tail -f temp.log $ jobs [1]+ Stopped (signal) tail -f temp.log

5. Conclusion

In this article, we got an understanding of interactive and non-interactive processes in Linux. We also discussed some of the operations we can perform with foreground and background processes. In the same context, we also learned different signals corresponding to some of the common Linux commands and controls that the kernel sends.

Источник

Оцените статью
Adblock
detector