Processes in Linux: Monitoring and Managing processes

published on Thu Apr 02 2020

In Linux, processes are how all programs or commands run. When a system starts up, the kernel starts a program called init. init runs shell scripts which start all the system services. Some of them run only in the background and do not have a user interface. They are called daemon processes. Programs launching other programes are called parent and child processes.

The kernel assigns process IDs in ascending order to processes with init getting PID 1 and tracks memory assigned and resume execution readiness for these processes. Since everything in Linux is a file, processes also have owners, user IDs, etc.

Viewing Processes

Using the ps command

ps command output snapshot

We can use the ps aux command to view a snapshot of currently running processes on the system. It shows us the processes with

Using the top command

top command output snapshot

The top command shows a continuously updating display of the system processes listed in order of process activity.

The output has two parts – a system summary followed by a list of processes sorted by CPU activity.

The summary fields are explained below:

Using the htop command

htop command output snapshot

An alternative to the top command is the htop command which is more interactive and has more options for filtering, sorting and much more. It also has, in general, a more friendly and appealing user interface.

Backgrounding Processes

To launch a program and immediately place it in the background, we follow the command with an ampersand (&) character.

<command> &

We can also launch a process, press Ctrl + z to pause it, and then resume it in the foreground using the fg command or in the background using the bg command.

We can use jobs to list the currently running background processes. The output would look something like:

[1] 31562

We can bring any background jobs to the foreground by using the fg command like this:

fg %<job_number>

Note that the job number here is different than the process ID. The job number in the above example is 1, between the square brackets whereas the 31562 is the process ID

Stopping Processes

We use the kill command to terminate processes by send a terminate signal to a process. We do it like so:

kill -signal <Process ID>

If no signal is specified, the TERM (terminate) signal is sent.

To send signals to multiple processes matching a specified program or username, we use the killall command like so:

killall [-u user] [-signal] name...

Possible Signals