Background and foreground in linux

Linux Cli: Background and Foreground Process

So we know there is a first process named ‘init’ with pid. This is parent
of all process in the system. And a process named ‘bash’ interacts with
Kernel on behalf of user requests or commands.

Now when I log in and type ps – I get below output:

$ ps PID TTY TIME CMD 5254 pts/1 00:00:00 bash 5336 pts/1 00:00:00 ps

See this is different from our previous output. Everytime you login,new
parent bash is created.In case our Manager Bash job id is 5254.
You know each command is a process, right? Lets create few process
for this session.

Did it hang for 5 seconds and then provide you the bash prompt again?

The way, our bash creates child process is blocking call. It means,
run the child process and wait for it to complete and then return
to me again.

Background process

So when we ran our child process (sleep), ‘Bash’ shell waited for
it to finish. User request below commands:

Shell will run sleep 5 first and wait for it to end. Then it
runs sleep 2 . But what if job-2 in this case ‘sleep 2’ is
more critical than first job. There is unnecessary delay of
5 seconds right? Shell has an option of running child process
on background – that means it wont wait for child to finish
before accepting inputs from user.

We can put any child process in background by just appending
& character to it!

Look, we can ran sleep 2 without hanging for 5 seconds! and the
number you see 5781 is the background child process id.
Its for our reference. How to verify its indeed the pid of child?
Simple just start child process in background and execute ‘ps’ command.

$ sleep 5 & [1] 6095 $ ps PID TTY TIME CMD 5254 pts/1 00:00:00 bash 6095 pts/1 00:00:00 sleep 6099 pts/1 00:00:00 ps

Our sleep 5 pid matches with ps output. Do we have better way to visualize
this? yes do. We have a command named ‘pstree’ which will tell you the
mapping between child and parent process!!

Lets try again, note down your Bash shell pid (here its 5254) $ pstree 5254 bash───pstree

It tells, we have Bash and one child process named ‘pstree’.run our child,

$ sleep 5 & [1] 6208 $ pstree 5254 bash─┬─pstree └─sleep

Now it says, ‘Bash’ has two child named, pstree and our background process ‘sleep’
pstree has an option to mention -p which displays pid next to process name.

$sleep 5 & [1] 6272 $ pstree -p 5254 bash(5254)─┬─pstree(6276) └─sleep(6272)

the pid we got while starting background process is same as what we got
from pstree output.

Lets say we have started 4 long running background jobs each runs for 145 seconds and
1 very long process for 3000 seconds.

$sleep 45 & [1] 6393 $sleep 45 & [2] 6397 $sleep 45 & [3] 6401 $sleep 45 & [4] 6406 $ sleep 3000 & [5] 6557 $pstree -p 5254 bash(5254)─┬─pstree(6410) ├─sleep(6393) ├─sleep(6397) ├─sleep(6401) └─sleep(6406) ├─sleep(6557)

List background jobs

pstree gives information about all jobs. We don’t need pstree(6410), because we are
interested in only background jobs. How to view only those jobs. For this purpose, we have ‘jobs’ command will give output like:

 jobs [1] Running sleep 145 & [2] Running sleep 145 & [3]- Running sleep 145 & [4]+ Running sleep 145 & [5]+ Running sleep 3000 &

It lists only our background processes and its status. So far good right?

Читайте также:  Macbook pro 2016 linux

Foreground process

Our process-[5] runs for 3000 seconds, it takes long time to complete.
Background will take less CPU time compared to non-background process
.ie foreground process. So lets bring to foreground process.
type, fg in our case, we need to bring background job-5.

Can you see shell hanging now? wait..hanging is a wrong word to use. shell executing
sleep command now? Are you going to wait for 3000 seconds? aka 50 minutes?

Lets admit it, by mistake without thinking, you (yes, its you !)brought this
background process to foreground, now you desperately want to put in on background again!!

Switch between foreground to background

Dont worry, Linux is so flexible we can do that too. Just press ‘ctrl+z’.You get
output which says job is stopped.

verify the status by checking output of jobs command.

$jobs [5]+ Stopped sleep 3000

Its stopped, we can restart the process again in background with ‘bg

$bg 5 [5]+ sleep 3000 & $jobs [5]+ Running sleep 3000 &

Great, Today we learned about background and foreground process and how to move
between them. We will discuss in upcoming lessons.

Источник

How to Work with Foreground and Background Process in Linux

An important concept to understand when working with the Linux process is what is the foreground and background process and how to control them. In Linux, if you execute any programs a process will be created with a unique ID (PID) and by default, the process runs in the foreground.

Let’s take a simple curl command – when you send a request to download a zip file over the internet, curl will run as a foreground process and all the outputs will be displayed in the terminal.

There are two important keystrokes that you have to understand before working with the background and foreground process.

  • CTRL+Z – This keystroke will stop the running process.
  • CTRL+C – This keystroke will kill the running process and free up memory in RAM.

Linux Foreground Process

A process when you start from the terminal by default runs as a foreground process. The foreground process will not allow you to use the terminal unless the process is completed. In this case, if you need access back to your terminal then you have to open a new terminal window or stop the running process or kill it.

It is also important to note, any process that is created via the terminal is attached to the terminal session and the process will be terminated once the terminal is closed. This is because bash is the parent process and any command you run in the terminal will be the child process to the bash so when you close the terminal parent process (bash) will automatically terminate all its child processes and its own.

Читайте также:  Self sign certificate linux

Below is an example of the foreground process. This is just a simple sleep command that will return the prompt to the user only after it is completed.

Linux Sleep Command

Now I have few options to gain control back to the terminal prompt. Either I have to cancel or stop the process or open a new terminal tab. When you stop a running process by pressing (CTRL+Z) you will get output as shown in the below image.

Stop Running Linux Process

To get the list of jobs either in running or stopped state you can run the below command.

List Running Linux Jobs

From the above image, you can see the jobs command gives you the process ID, what command you submitted, what is the status of it. You will also get a job ID for every process that you submit ( [1], [2] [3], etc..).

To start a job that is in the stopped state or bring the background job to the foreground run the following command.

Where %4 denotes the job ID that you can get from the “jobs -l” command.

Start Job in Foreground

Linux Background Process

Background process runs your process in the background and will not take control of your terminal prompt. You can start a session and you are free to use your terminal. To submit a command as a background process you have to add & symbol at the end of the command.

Run Linux Command in Background

Run jobs command to get the list of jobs.

From the below image you can see Job ID [5] is assigned to the job and & symbol tells it is submitted and running as a background job.

List Running Linux Comamnds (Jobs)

You can start any stopped job in the background directly by running the following command.

Where %2 denotes the job ID that you can get from the “jobs -l” command.

Run Linux Command in Background

That’s it for this article. If there is any feedback or suggestion leave it in the comment box.

Источник

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.

Читайте также:  Linux service permission denied

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.

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.

Источник

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