...

Find out what processes are running in the background on Linux nixCraft Updated Tutorials/Posts

find-out-what-processes-are-running-in-the-background-on-linux-nixcraft-updated-tutorials-posts

I am a new CentOS Linux server sysadmin. How do I find out what processes are running in the background on Linux operating systems?

In Linux, a background process is nothing but process running independently of the shell. One can leave the terminal window and, but process executes in the background without any interaction from users. For example, Apache or Nginx web server always runs in the background to serve you images and dynamic content. This page shows how to list all such process running in the background in Linux.

How to find out what processes are running in the background

  1. 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.
  2. 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.
  3. 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

Run the jobs command:
jobs

[1]+ Stopped vim
[2]- Running sleep 10000 &

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

Find out what processes are running in the background on Linux
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 do I list only running processes on Linux?

ps r

List all processes on this current terminal

ps T

Display Linux processes without controlling ttys

ps x

Linux background processes command

top and htop commands

The top command displays Linux processes in real-time. Run it as follows and look for STAT (s) column.
top
top [option]

Linux top command
Similarly one can use an enhanced version of the top like atop or htop.
htop
atop
htop [options]

Linux htop command

Conclusion

This page explained various Linux commands to find out what processes are running in the background. You learned how to use the ps command to get a snapshot of the current Linux processes and use various STATS to interpret the results.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Discover more from WIREDGORILLA

Subscribe now to keep reading and get access to the full archive.

Continue reading