How To Get A Linux Program To Run In The Background
Use the Fustigate crush in Linux to manage foreground and groundwork processes. You lot can apply Fustigate's job control functions and signals to requite you lot more flexibility in how y'all run commands. We prove you lot how.
All About Processes
Whenever a program is executed in a Linux or Unix-like operating arrangement, a process is started. "Procedure" is the proper name for the internal representation of the executing program in the computer'south memory. There is a process for every agile program. In fact, there is a process for nearly everything that is running on your computer. That includes the components of your graphical desktop surround (GDE) such as GNOME or KDE, and system daemons that are launched at start-upward.
Why nearly everything that is running? Well, Fustigate built-ins such equally cd, pwd, and allonym exercise not need to have a procedure launched (or "spawned") when they are run. Fustigate executes these commands inside the example of the Bash shell that is running in your terminal window. These commands are fast precisely considering they don't need to take a procedure launched for them to execute. (You tin type help
in a terminal window to encounter the list of Fustigate congenital-ins.)
Processes tin be running in the foreground, in which case they take over your last until they accept completed, or they can exist run in the groundwork. Processes that run in the background don't boss the terminal window and you tin can keep to work in information technology. Or at to the lowest degree, they don't dominate the final window if they don't generate screen output.
A Messy Example
Nosotros'll beginning a elementaryping
trace running. We're going to ping
the How-To Geek domain. This volition execute equally a foreground procedure.
ping world wide web.howtogeek.com
Nosotros get the expected results, scrolling downwards the terminal window. Nosotros tin't practise anything else in the terminal window while ping
is running. To terminate the command hit Ctrl+C
.
Ctrl+C
The visible effect of the Ctrl+C
is highlighted in the screenshot. ping
gives a short summary and and then stops.
Let's echo that. But this fourth dimension we'll striking Ctrl+Z
instead of Ctrl+C
. The task won't exist terminated. It will get a background task. Nosotros get control of the terminal window returned to us.
ping www.howtogeek.com
Ctrl+Z
The visible effect of striking Ctrl+Z
is highlighted in the screenshot.
This fourth dimension we are told the procedure is stopped. Stopped doesn't hateful terminated. It'due south like a machine at a stop sign. We oasis't scrapped it and thrown it away. It'due south still on the road, stationary, waiting to get. The procedure is now a background job.
The jobs
control will listing the jobs that have been started in the current terminal session. And because jobs are (inevitably) processes, we tin can also use the ps
command to see them. Let's use both commands and compare their outputs. We'll use the T
option (terminal) selection to only list the processes that are running in this last window. Annotation that there is no need to use a hyphen -
with the T
option.
jobs
ps T
The jobs
command tells u.s.:
- [i]: The number in square brackets is the job number. We tin 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 task that will be acted upon if we use a task control control without a specific task number. It is called the default job. The default job is always the one well-nigh recently added to the list of jobs. - Stopped: The process is non running.
- ping www.howtogeek.com: The command line that launched the procedure.
The ps
control tells usa:
- PID: The procedure ID of the process. Each process has a unique ID.
- TTY: The pseudo-teletype (last window) that the process was executed from.
- STAT: The status of the procedure.
- Fourth dimension: The amount of CPU time consumed past the process.
- COMMAND: The command that launched the process.
These are mutual values for the STAT column:
- D: Uninterruptible sleep. The process is in a waiting land, ordinarily waiting for input or output, and cannot be interrupted.
- I: Idle.
- R: Running.
- South: Interruptible sleep.
- T: Stopped by a job control signal.
- Z: A zombie process. The process has been terminated but hasn't been "cleaned down" past its parent process.
The value in the STAT column can be followed by one of these extra indicators:
- <: High-priority task (non nice to other processes).
- N: Low-priority (overnice to other processes).
- L: process has pages locked into retention (typically used past real-time processes).
- s: A session leader. A session leader is a process that has launched process groups. A shell is a session leader.
- l: Multi-thread process.
- +: A foreground procedure.
We tin see that Bash has a state of Ss
. The capital letter "S" tell us the Bash shell is sleeping, and it is interruptible. As shortly every bit we need it, it will respond. The lowercase "s" tells us that the trounce is a session leader.
The ping command has a state of T
. This tells us that ping
has been stopped by a job control indicate. In this example, that was the Ctrl+Z
we used to put it into the groundwork.
The ps T
command has a country of R
, which stands for running. The +
indicates that this process is a member of the foreground group. So the ps T
command is running in the foreground.
The bg Command
The bg
control is used to resume a background process. It tin can be used with or without a job number. If y'all use it without a job number the default job is brought to the foreground. The procedure still runs in the background. You cannot send whatever input to information technology.
If we consequence the bg
command, nosotros will resume our ping
command:
bg
The ping
command resumes and we see the scrolling output in the terminal window again. The name of the control that has been restarted is displayed for you. This is highlighted in the screenshot.
Just we have a trouble. The chore is running in the background and won't take input. And so how do we stop it? Ctrl+C
doesn't exercise anything. We can meet it when nosotros blazon it but the groundwork task doesn't receive those keystrokes so it keeps pinging merrily away.
In fact, we're now in a strange composite style. We can type in the terminal window but what we type is quickly swept away by the scrolling output from the ping
command. Anything nosotros type takes upshot in the foregound.
To stop our background task we need to bring it to the foreground then end information technology.
The fg Command
The fg
command will bring a groundwork task into the foreground. Just like the bg
control, it can be used with or without a job number. Using it with a task number means information technology volition operate on a specific job. If it is used without a job number the final command that was sent to the background is used.
If we type fg
our ping
command will be brought to the foreground. The characters we type are mixed up with the output from the ping
command, but they are operated on by the trounce equally if they had been entered on the command line equally usual. And in fact, from the Bash beat's point of view, that is exactly what has happened.
fg
And now that nosotros accept the ping
command running in the foreground over again, we can utiliseCtrl+C
to kill it.
Ctrl+C
Nosotros Need to Send the Right Signals
That wasn't exactly pretty. Plain running a procedure in the background works best when the process doesn't produce output and doesn't require input.
Simply, messy or not, our example did accomplish:
- Putting a procedure into the background.
- Restoring the process to a running state in the background.
- Returning the process to the foreground.
- Terminating the procedure.
When yous employCtrl+C
and Ctrl+Z
, you are sending signals to the process. These are autograph means of using the kill
control. There are 64 different signals that kill
can send. Use kill -50
at the command line to list them. kill
isn't the only source of these signals. Some of them are raised automatically by other processes within the arrangement
Here are some of the ordinarily used ones.
- SIGHUP: Signal 1. Automatically sent to a process when the terminal it is running in is airtight.
- SIGINT: Indicate two. Sent to a process y'all hit
Ctrl+C
. The process is interrupted and told to terminate. - SIGQUIT: Signal 3. Sent to a process if the user sends a quit signal
Ctrl+D
. - SIGKILL: Indicate 9. The process is immediately killed and will not try to close downwards cleanly. The process does not become down gracefully.
- SIGTERM: Indicate 15. This is the default signal sent by
kill
. It is the standard program termination signal. - SIGTSTP: Indicate 20. Sent to a process when you use
Ctrl+Z
. Information technology stops the process and puts it in the background.
We must use the kill
command to issue signals that exercise not have fundamental combinations assigned to them.
Further Job Command
A process moved into the background by using Ctrl+Z
is placed in the stopped state. Nosotros have to use the bg
command to starting time it running over again. To launch a plan as a running background procedure is simple. Append an ampersand &
to the end of the command line.
Although it is best that background processes do not write to the terminal window, we're going to use examples that do. Nosotros need to accept something in the screenshots that we tin refer to. This command will showtime an endless loop as a background process:
while true; do echo "How-To Geek Loop Process"; sleep 3; washed &
Nosotros are told the task number and process ID id of the procedure. Our job number is 1, and the process id is 1979. We can use these identifiers to command the process.
The output from our endless loop starts to appear in the final window. As earlier, we tin use the command line merely any commands we effect are interspersed with the output from the loop procedure.
ls
To cease our procedure we can use jobs
to remind ourselves what the task number is, and and so use kill
.
jobs
reports that our process is job number 1. To employ that number with kill
we must precede information technology with a pct sign %
.
jobs
kill %one
RELATED: How Linux Signals Piece of work: SIGINT, SIGTERM, and SIGKILL
kill
sends the SIGTERM
signal, signal number xv, to the process and it is terminated. When the Enter key is adjacent pressed, a status of the task is shown. It lists the process equally "terminated." If the process does not respond to the impale
command you can take information technology upwards a notch. Employ kill
with SIGKILL
, indicate number 9. Simply put the number 9 betwixt the kill
command the chore number.
kill 9 %1
Things We've Covered
- Ctrl+C: Sends
SIGINT
, bespeak 2, to the process—if it is accepting input—and tells it to terminate. - Ctrl+D: Sends
SISQUIT
, betoken 3, to the procedure—if it is accepting input—and tells it to quit. - Ctrl+Z: Sends
SIGSTP
, point 20, to the process and tells it to end (append) and go a background process. - jobs: Lists the background jobs and shows their job number.
- bg job_number: Restarts a background procedure. If you don't provide a chore number the last process that was turned into a background task is used.
- fg job_number: brings a background process into the foreground and restarts it. If you don't provide a job number the last process that was turned into a background job is used.
- commandline &: Adding an ampersand
&
to the finish of a command line executes that command equally a background task, that is running. - kill % job_number: Sends
SIGTERM
, bespeak 15, to the process to terminate it. - impale 9 % job_number: Sends
SIGKILL
, signal 9, to the process and terminates information technology abruptly.
RELATED: How to Kill Processes From the Linux Terminal
How To Get A Linux Program To Run In The Background,
Source: https://www.howtogeek.com/440848/how-to-run-and-control-background-processes-on-linux/
Posted by: dooleycitage.blogspot.com
0 Response to "How To Get A Linux Program To Run In The Background"
Post a Comment