- Linux: Start Command In Background
- Syntax
- Examples
- How do I see jobs running in the background?
- How do I kill the jobs running in the background?
- How do I bring process running in the background to the foreground?
- Summary of all useful commands
- How to Start Linux Command in Background and Detach Process in Terminal
- How to Start a Linux Process or Command in Background
- Keep Linux Processes Running After Exiting Terminal
- Detach a Linux Processes From Controlling Terminal
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How to Run Bash Commands in the Background in Linux
- End a Command with &
- & After a Command, Then Disown It
- & After a Command with /dev/null
- Nohup, with & and /dev/null
- Running Linux Commands in Background and Foreground
- Start a Linux process in background directly
- Send a running Linux process to background
- See all processes running in background
- Bring a Process to Foreground in Linux
Linux: Start Command In Background
[donotprint]
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Bash/ksh Linux |
Est. reading time | 10m |
[/donotprint]
Syntax
You can put a task (such as command or script) in a background by appending a & at the end of the command line. The & operator puts command in the background and free up your terminal. The command which runs in background is called a job. You can type other command while background command is running. The syntax is:
Examples
Put the ls command in the background, enter:
$ ls *.py > output.txt &
Put the following find command in a background by putting a ‘&’ at the end of the command line:
Fig.01: Linux background job in action (click to enlarge)
How do I see jobs running in the background?
Type the following command:
jobs
Sample outputs:
To see process IDs for JOB IDs in addition to the normal information pass the -l option:
jobs -l
Sample outputs:
To see process IDs only, enter:
jobs -p
Sample outputs:
How do I kill the jobs running in the background?
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How do I bring process running in the background to the foreground?
The syntax is:
%JOB-ID
OR
fg JOB-ID
First, list the current jobs with jobs command, enter:
jobs -l
Sample outputs:
To bring the job id #2 to the foreground, enter:
%2
OR use fg command:
fg 2
Sample outputs:
To send back this job in the background hit CTRL-Z i.e. while holding the CTRL key, press z key. This will suspend the current foreground job. Type the following command to send back the job in the background:
%2 &
OR use bg command:
bg
The grep command job is now running in the background.
Summary of all useful commands
Description | Command |
---|---|
To see which jobs are still running jobs | jobs jobs -l ps aux |
To put a command / script to the background | command & /path/to/command & /path/to/script arg1 & |
To bring a background job to the foreground | fg n %n |
To send a job to the background without canceling it | bg n %n & |
Note: n == Job id (use jobs command to see job id)..
See also:
- Putting jobs in background from the Linux shell scripting tutorial.
- Command examples pages: jobs command, bg command, and fg command
- Man pages: ksh(1)
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to Start Linux Command in Background and Detach Process in Terminal
In this guide, we shall bring to light a simple yet important concept in process handling in a Linux system, that is how to completely detach a process from its controlling terminal.
When a process is associated with a terminal, two problems might occur:
- your controlling terminal is filled with so much output data and error/diagnostic messages.
- in the event that the terminal is closed, the process together with its child processes will be terminated.
To deal with these two issues, you need to totally detach a process from a controlling terminal. Before we actually move to solve the problem, let us briefly cover how to run processes in the background.
How to Start a Linux Process or Command in Background
If a process is already in execution, such as the tar command example below, simply press Ctrl+Z to stop it then enter the command bg to continue with its execution in the background as a job.
You can view all your background jobs by typing jobs . However, its stdin, stdout, stderr are still joined to the terminal.
Run Linux Command in Background
You can as well run a process directly from the background using the ampersand, & sign.
Start Linux Process in Background
Take a look at the example below, although the tar command was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.
Linux Process Running in Background Message
Keep Linux Processes Running After Exiting Terminal
We will use disown command, it is used after the a process has been launched and put in the background, it’s work is to remove a shell job from the shell’s active list jobs, therefore you will not use fg , bg commands on that particular job anymore.
In addition, when you close the controlling terminal, the job will not hang or send a SIGHUP to any child jobs.
Let’s take a look at the below example of using diswon bash built-in function.
Keep Linux Process Running After Closing Terminal
You can also use nohup command, which also enables a process to continue running in the background when a user exits a shell.
Put Linux Process in Background After Closing Shell
Detach a Linux Processes From Controlling Terminal
Therefore, to completely detach a process from a controlling terminal, use the command format below, this is more effective for graphical user interface (GUI) applications such as firefox:
In Linux, /dev/null is a special device file which writes-off (gets rid of) all data written to it, in the command above, input is read from, and output is sent to /dev/null.
As a concluding remark, provided a process is connected to a controlling terminal, as a user, you will see several output lines of the process data as well as error messages on your terminal. Again, when you close the a controlling terminal, your process and child processes will be terminated.
Importantly, for any questions or remarks on the subject, reach us by using the comment form below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
How to Run Bash Commands in the Background in Linux
There’s nothing more annoying than running a command in your terminal and having it run for minutes, sometimes hours, and not be able to use your terminal again. Sure, you can use tabs, but that’s a clunky solution, and it’s not always optimal because you may want to see updates as you’re working. Here we show you a few different ways to run bash commands in the background in Linux.
Also read: How to Use lsof Command in Linux to List Open Files
End a Command with &
If you want to push a command into the background, using & at the end is an easy way to do that. This way, you can issue a command in the background and continue to use your terminal as it runs. It comes with a catch, though. Using & doesn’t disconnect the command away from you; it just pushes it into the background. This means that while you’re trying to use the terminal, anything the command wants to push to STDOUT or STDERR will still be printed, which may be distracting.
When the terminal session is closed, the command ends. You can also kill the command by issuing the jobs command, finding the number of the command that’s running, and killing it with the kill command. That syntax is as follows:
Using & is good if you need to push something off for a bit but don’t expect it to continue forever.
& After a Command, Then Disown It
Running a command with just & pushes it off to the back and keeps it running as long as the terminal window is open. If, however, you’re looking to keep this command running in constant, even with your terminal session ending, you can use the disown command.
To use this method, start by adding an & .
As mentioned above, using & pushes this command into the background but doesn’t detach it from your user. You can verify this by typing jobs into the terminal. It’ll show the command running in the background as we saw before.
Just type disown into the shell, and it’ll do just that. (And you can once again verify this with the jobs command.)
You can just make out the disown command in there
Now you can close your terminal and continue about your day. It’ll still keep piping things to STDOUT or STDERR , but once you exit and reopen your terminal, you won’t see anything there. You can find the command again with the top or ps commands and kill it with the kill command.
The disowned job is the second one, with the PID 16238.
& After a Command with /dev/null
Adding & after a command will push a command into the background, but as a result, the background command will continue to print messages into the terminal as you’re using it. If you’re looking to prevent this, consider redirecting the command to /dev/null .
This does not prevent the command from closing when the terminal closes. However, as mentioned above, it’s possible to use disown to disown the running command away from the user. You can also kill it in either of the methods mentioned above if you don’t want it to run anymore.
Nohup, with & and /dev/null
Unlike the previous commands, using nohup allows you to run a command in the background and keep it running. How? nohup bypasses the HUP signal (signal hang up), making it possible to run commands in the background even when the terminal is off. Combine this command with redirection to “/dev/null” (to prevent nohup from making a nohup.out file), and everything goes to the background with one command.
Also read: What Is Nohup and How Do You Use It?
Most terminal programs on Linux today have features built in to allow them to run in the background with little effort. Along with that, modern init systems (like systemd) can allow users to start programs like services at boot or whenever.
Still, some programs on Linux lack the ability to run as a daemon or integrate with modern init systems. This is a real inconvenience but is understandable, as not all developers have the skill or time to add new features.
Luckily, commands like nohup or disown are still a reality and can close the gap in moving programs like this to the background. They’re not perfect or fancy, but they get the job done when needed.
If you enjoyed this Linux article, make sure to check out some of our other Linux content, like how to connect your Google account to GNOME Shell, the best Linux distros for windows users, and LS commands you need to know.
John is a young technical professional with a passion for educating users on the best ways to use their technology. He holds technical certifications covering topics ranging from computer hardware to cybersecurity to Linux system administration.
Источник
Running Linux Commands in Background and 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 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 background. I’ll also show you how to bring the background processes back to foreground.
Start a Linux process in 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 background itself.
To run a Linux command in background, all you have to do is to add ampersand (&) at the end of the command, like this:
Let’s take a simple bash sleep command and send it to background.
When the command finishes in the background, you should see information about that on the terminal.
Send a running Linux process to background
If you already ran a program and then realized that you should have run it in background, don’t worry. You can send a running process to 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 background. The suspended process will now run in background.
Let’s take the same example as before.
See all processes running in background
Now that you know how to send the processes in 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:
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 background in Linux. But what about bringing a process running in the background to foreground again?
To send the command to background, you used ‘bg’. To bring 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 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.
That’s it
This was a quick one but enough for you to learn a few things about running commands in background in Linux. I would advise learning nohup command as well. This command lets you run commands in background even after you log out of the session.
If you have questions or suggestions, please leave a comment below.
Источник