- Find out what processes are running in the background on Linux
- How to find out what processes are running in the background
- How can I run a Linux process in the background?
- List your background processes
- Linux background processes list command
- Understanding ps command outputs
- Is my Linux process running in the foreground or background?
- 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:
- 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
- How to Run a Process in the Background on Linux
- Placing a Running Foreground Process into the Background
- Placing a Running Background Process into the Foreground
- Starting a Process in the Background
Find out what processes are running in the background on Linux
How to find out what processes are running in the background
- You can use the ps command to list all background process in Linux. Other Linux commands to obtain what processes are running in the background on Linux.
- top command – Display your Linux server’s resource usage and see the processes that are eating up most system resources such as memory, CPU, disk and more.
- htop command – Just like a top command but with an improved user interface.
Let us see both traditional command and modern commands examples that one can use to manage running processes in Linux.
How can I run a Linux process in the background?
To run your process or command/shell script in the background, include an & (an ampersand) at the end of the command/shell script you use to run the job. For example:
command &
/path/to/script &
sleep 10000 &
List your background processes
To stop the foreground process press CTRL + z . One can refers to the background process or stopped process by number. For example, vim is stopped and has 1 as number, so run the bg command to restart a stopped background process:
bg %n
bg %1
One can bring a background process to the foreground such as sleep command using the fg command:
fg %n
fg %2
Finally, kill a running process named “sleep 10000” using the kill command:
kill %n
kill %2
Linux background processes list command
Open the terminal application and issue the following ps command command to show all running process on the system including those running in the background:
$ sudo ps -aux | less
OR
# ps aux | more
List all running processes on Linux using ps command
Understanding ps command outputs
The first column shows the user name who started the foreground or background processes on Linux system. For example, the daemon user started the atd process. The process name itself displayed in the last column. The STAT coloum gives us the state of a Linux process:
Process STATE code | Description |
---|---|
D | uninterruptible sleep (usually IO) |
I | Idle kernel thread |
R | running or runnable (on run queue) |
S | interruptible sleep (waiting for an event to complete) |
T | stopped by job control signal |
t | stopped by debugger during the tracing |
W | paging (not valid since the 2.6.xx kernel) |
X | dead (should never be seen) |
Z | defunct (“zombie”) process, terminated but not reaped by its parent |
Typically process in “interruptible sleep” are running in the background and shows a “ S ” on processes STAT column. The interruptible sleep means the process can be terminated or killed with the help of kill command. On the other hand, processes in a “D” or uninterruptible sleep state are usually waiting on I/O. Therefore, you cannot kill “D” state processes as they are uninterruptible. Additional characters may be displayed as follows too:
Process STATE code | Description |
---|---|
high-priority (not nice to other users) | |
N | low-priority (nice to other users) |
L | has pages locked into memory (for real-time and custom IO) |
s | is a session leader |
l | is multi-threaded (using CLONE_THREAD, like NPTL pthreads do) |
+ | is in the foreground process group |
Is my Linux process running in the foreground or background?
Based upon the above tables, one can determine if Linux process in background or foreground or running and so on.
Command/ Process | ps STATE code | Foreground or Background? |
---|---|---|
/sbin/init | Ss | Background process (interruptible sleep and a session leader) |
/usr/sbin/rsyslogd -n | Ssl | Background process (interruptible sleep+a session leader and multi-threaded app) |
/sbin/agetty —noclear | Ss+ | Background process (interruptible sleep and a session leader and is in foreground group) |
ps aux | R+ | Running foreground process |
Use the following command to list Linux processes along with pid, user name, stat as follows:
ps -eo pid,user,stat,comm
You can combine ps with grep command command as follows:
ps -eo pid,user,stat,comm | grep nginx
Источник
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.
Источник
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.
Источник
How to Run a Process in the Background on Linux
In this tutorial you are going to learn how to run a process in the background. You will also learn how to move a background process into the foreground, and vice versa.
When we execute a command in Linux from a shell the process it creates run in the foreground, attaching itself to our session. This means mean we will not be able to interact with our shell until the process exits.
While this may be desirable for short-lived processes, such as running a find on your filesystem. It isn’t always desirable for longer lived processes, such as running a development web server.
Placing a Running Foreground Process into the Background
A foreground process is the a process. In order to place a foreground proces into the background, we must first put the process to sleep, and then place it in the background.
- Execute the command to run your process.
- Press CTRL+Z to put the process into sleep.
- Run the bg command to wake the process and run it in the backround.
Placing a Running Background Process into the Foreground
A background process can be return to the foreground if needed. We use the fg command to achieve this.
Running the fg command on its own, without a process ID, will force your last execute process from your session into the foreground.
To foreground a specific process you should include the process ID.
Starting a Process in the Background
Finally, a process created by executing a command from the command-line can be forced to start in the background. To so we append an ampersign (&) at the end of our command. This instructs the shell, such as Bash, to start the process in the background.
You will be given the Job ID of the process and return to an interactive shell.
Источник