How to run and control background processes in Linux

Contents

A shell environment on a Linux computer.

Use the Bash shell on Linux to manage foreground and background processes. You can use Bash job control signals and functions to give you more flexibility in how you run commands. We show you how.

All about the processes

Whenever a program runs on a Linux or Unix-like operating system, a procedure is started. "Procedure" is the name of the internal representation of the running program in the computer's memory.. There is a procedure for each active program. Actually, there is a procedure for almost everything that runs on your computer. That includes the components of your graphical desktop environment (WHERE) What GNOME O WHEREand system got damn that are launched at the beginning.

Why narrowly everything is running? Well, Bash embedded as CD, pwd, and alias it is not necessary to have a procedure started (O “generated”) when they run. Bash runs these commands within the Bash shell instance that runs in your terminal window. These commands are fast exactly because they don't need to have a procedure started in order for them to run.. (You can type help in a terminal window to see the list of built-in Bash functions).

Processes can run in the foreground, in which case they will seize your terminal until they have been completed, or they can run in the background. Processes running in the background do not dominate the terminal window and you can continue working in it. Or at least, they don't dominate the terminal window if they don't generate a screen output.

A messy example

We will start a simple ping tracking running. We'll go to ping el dominio How-To Geek. This will run as a foreground procedure.

ping www.systempeaker.com

We obtain the expected results, scrolling down the terminal window. We can't do anything else in the terminal window while ping Is running. To end the command, press Ctrl+C.

Ctrl+C

The visible effect of Ctrl+C is highlighted in the screenshot. ping gives a short summary and then stops.

Let's repeat that. But this time we will hit Ctrl+Z instead of Ctrl+C. The task will not be finished. It will become a background task. We get control of the terminal window that is returned to us.

ping www.systempeaker.com
Ctrl+Z

The visible effect of hitting Ctrl+Z is highlighted in the screenshot.

This time we are told that the procedure stops. Stopped doesn't mean finished. It's like a car on a stop sign. We haven't thrown it away and thrown it away. Still on the road, stopped, waiting to start. The procedure is now a background job.

the jobs command will list the jobs that have been started in the current terminal session. And since the jobs are (inevitably) processes, at the same time we can use the ps command to see them. Let's use both commands and compare their outputs. We will use the T option (terminal) option to list only the processes that are running in this terminal window. Note that it is not necessary to use a hyphen - with the T option.

jobs
ps T

the jobs command tells us:

  • [1]: The number in brackets is the job number. We can use this to refer to the job when we need to control it with job control commands.
  • +: The plus sign + shows that this is the job that will be acted upon if we use a job control command without a specific job number. It's called default job. The default job is always the one that was most recently added to the job list.
  • Disrupted: The procedure is not running.
  • ping www.systempeaker.com: The command line that started the procedure.

the ps command tells us:

  • PID: The procedure ID of the procedure. Each procedure has a unique identification.
  • TTY: The pseudo-teletype (terminal window) from which the procedure was run.
  • STATISTICS: The status of the procedure.
  • WEATHER: The amount of CPU time consumed by the procedure.
  • COMMAND: The command that started the procedure.

These are common values ​​for the STAT column:

  • D: Uninterrupted sleep. The procedure is in a waiting state, generally waiting for entry or exit, and it can't be interrupted.
  • I: Inactive.
  • R: In a hurry.
  • S: Interruptible dream.
  • T: Stopped by a work control signal.
  • WITH: A zombie procedure. The procedure is over, but your main procedure has not “cleaned”.

The value in the STAT column can be followed by one of these additional indicators:

  • <: High priority task (not pleasant for other processes).
  • North: Low priority (good for other processes).
  • L: procedure has pages locked in memory (regularly used by real-time processes).
  • s: Session leader. A session leader is a procedure that has launched process groups. A shell is a session leader.
  • l: Multi-threaded procedure.
  • +: A close-up procedure.

We can see that Bash has a state of Ss. The capital "S" tells us that the Bash shell is sleeping and is interruptible. As soon as we need it, answer to. The “s” lowercase tells us that the shell is a session leader.

The ping command has a status of T. This tells us that ping has been stopped by a work control signal. In this example, that was the Ctrl+Z we used to put it in the background.

the ps T the command has a status of R, what does running mean. the + indicates that this procedure is a member of the foreground group. So he ps T The command is running in the foreground.

The bg command

the bg The command is used to resume a background procedure. Can be used with or without job number. If you use it without a work number, default job comes to the fore. The procedure is still running in the background. Can't send you any input.

If we issue the bg command, we will resume our ping command:

bg

the ping The command resumes and we see the scrolling output in the terminal window once more. The name of the command that has been restarted is displayed. This is highlighted in the screenshot.

background ping process resumed with exit on a terminal widow

But we have a hindrance. The task is running in the background and is not accepting input. Then, How do we stop it? Ctrl+C does nothing. We can see it when we write it, but the background task is not getting those keystrokes, so it keeps pinging happily.

Actually, now we're in a strange combo mode. We can write in the terminal window, but what we write is quickly swept by the displacement output of the ping command. Everything we write takes effect in the foreground.

To stop our background task, we must put it in the foreground and then stop it.

The fg command

the fg The command will bring a background task to the foreground. As the bg command, can be used with or without a job number. Using it with a job number means it will work on a specific job. If used without a job number, the last command that was sent to the background is used.

If we write fg our ping The command will come to the fore. The characters we type are mixed with the output of the ping command, but they are operated by the shell as if they had been entered on the command line as usual. And in reality, from the point of view of the Bash shell, that's exactly what has happened.

fg

And now that we have the ping command running in the foreground once again, we can use Ctrl+C to kill it.

Ctrl+C

We need to send the right signals

That wasn't exactly pretty. Evidently, running a procedure in the background works best when the procedure produces no output and needs no input.

But, messy or not, our example achieved:

  • Put a background procedure.
  • Restore the procedure to a background running state.
  • Bringing the procedure back to the foreground.
  • Finishing the procedure.

When you use Ctrl+C and Ctrl+Z , is sending signals to the procedure. These are shorthand forms to use the kill command. exist 64 different signals that kill You can send. To use kill -l on the command line to list them. kill is not the only source of these signals. Some of them are generated automatically by other processes within the system.

Here are some of the most used.

  • SIGHUP: Sign 1. Automatically sent to a procedure when the terminal it is running in is closed.
  • SIGINT: Sign 2. Sent to a procedure that you get right. Ctrl+C. The procedure is interrupted and you are instructed to end.
  • SIGQUIT: Sign 3. Sent to a procedure if the user sends a signal to quit smoking Ctrl+D.
  • SIGKILL: Sign 9. The procedure stops immediately and will not attempt to shut down cleanly. The procedure does not unfold gracefully.
  • SIGTERM: Sign 15. This is the default signal sent by kill. It is the standard program termination signal.
  • SIGTSTP: Sign 20. Sent to a procedure when you use Ctrl+Z. Stops the procedure and puts it in the background.

We must use the kill command to output signals that do not have key combinations assigned.

Greater control of work

A procedure went into the background through the use of Ctrl+Z is placed in the stopped state. We have to use the bg command to get it working again. Starting a program as a procedure running in the background is easy. Add an ampersand & to the end of the command line.

Even though it is better that the background processes do not write to the terminal window, we will use examples that do. We need to have something in the screenshots that we can reference. This command will start an endless loop as a background procedure:

as long as it's true; echo “How to do the geek loop procedure”; to sleep 3; done &

We are told the job number and procedure identification ID of the procedure. Our work number is 1 and the identification of the procedure is 1979. We can use these identifiers to control the procedure.

The output of our endless loop starts to appear in the terminal window. As before, we can use the command line, but all the commands we issue are interspersed with the output of the loop procedure.

ls

output of the background loop process interspersed with the output of other commands

To stop our procedure we can use jobs to remind us what the job number is and then use kill.

jobs informs that our procedure is job number 1. To use that number with kill we must precede it with a percent sign %.

jobs
kill %1

RELATED: How Linux Signals Work: SIGINT, SIGTERM y SIGKILL

kill sends the SIGTERM sign, sign number 15, to the procedure and it ends. The next time the Enter key is pressed, job status is displayed. List the procedure as “finished”. If the procedure does not respond to the kill command can take it up a notch. To use kill with SIGKILL , sign number 9. Just enter the number 9 Come in kill command job number.

kill 9 %1

Things we have covered

  • Ctrl + C: Send SIGINT, sign 2, to the procedure, if you are accepting input, and tells him to finish.
  • Ctrl + D: Send SISQUIT, sign 3, to the procedure - if you are accepting input - and tell you to exit.
  • Ctrl + WITH: Send SIGSTP, sign 20, to the procedure and tells you to stop (suspend) and become a background procedure.
  • works: List background jobs and show their job number.
  • bg work number: Restart a background procedure. If you don't provide a work number, the last procedure that became a background task is used.
  • fg work number: brings a background procedure to the foreground and restarts it. If you don't provide a work number, the last procedure that became a background task is used.
  • command line AND: Add an ampersand & at the end of a command line run that command as a background task, what is running.
  • to kill %work number: Send SIGTERM, sign 15, to the procedure to finish it.
  • to kill 9 %work number: Send SIGKILL, sign 9, to the procedure and ends it abruptly.

RELATED: How to kill processes from the Linux terminal

setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.