Top Process Management Commands in Linux?

By Raju

April 7, 2017

linux commands

When you run any application or program on a Linux server, it creates an instance called process. In this article we'll discuss following process management commands one by one. These are TOP, HTOP, PS, PSTREE, PGREP, NICE, RENICE , and KILL command.

Each process uses server resources like memory, storage and computing resources to finish the job associated with each process.

Every time a new process is started it is assigned an identification number (ID) which is called process ID or PID for short.

As as Linux Administrator or user, you’ll run into situations where you may need to manage all these processes. That’s where the Linux operating system provides a lot of tools to manage running processes.

Here are the few scenarios where you’ll run into.

  • List top running processes and server resource snapshot
  • To view all the running processes on Linux
  • To see resources used by each process
  • Change process priorities and resources available to processes.
  • Find specific process and change state of that process
  • Killing running or run away processes

You can use Linux process management commands or tools to handle all the above situations.

top command in Linux or Ubuntu

The most easiest and essential command for every Linux user is TOP. As the name indicates it displays overall view of the Linux system including TOP running processes.

top command in Linux

You can see server statistics like uptime, load averages and running processes states in the first few lines.

It also shows top running processes with their process identification number(PID), process user, Priority, nice value, %CPU and %memory consumed by the process etc.

htop command in Linux

Htop is similar to TOP but more advanced with an interactive process viewer. It shows CPU,Memory and SWAP resources as text graphs.

htop command in Linux

Comparison between htop and classic top

  • In htop you can scroll the list vertically and horizontally to see all processes and full command lines.
  • In top you are subject to a delay for each unassigned key you press (especially annoying when multi-key escape sequences are triggered by accident).
  • htop starts faster (top seems to collect data for a while before displaying anything).
  • In htop you don't need to type the process number to kill a process, in top you do.
  • In htop you don't need to type the process number or the priority value to renice a process, in top you do.
  • In htop you can kill multiple processes at once.
  • top is older, hence, more tested.

ps command to List Processes

"ps" command is the most basic command to see all the running processes on the system. It outputs all running processes of specific user or all users.

To see running processes of existing user:

ps

The output will be similar to this:

PID TTY TIME CMD
81775 pts/0 00:00:00 bash
90379 pts/0 00:00:00 ps

To see running processes of all users:

ps -aux

To view specific service/user running processes:

ps -ef | grep httpd

pstree command in Linux

pstree” command will be useful if you want to see running processes hierarchically arranged in tree format.

It’s easy to understand parent and child processes using this command. It is used as a more visual alternative to the ps command. The root of the tree is either init or the process with the given pid.

nice command to change process priorities

nice command is used to assign particular priority, thus giving the process more or less CPU time than other processes.

Process priority values range from -20 to 19. Lower the nice value, higher the priority. The default nice value is 0.

It means -20 has highest priority over 15. In order use higher priorities (negative nice values) administrator privileges are required.

When you use the command nice you start a new process and assign it priority (nice) value at the same time.

nice -3 top

The above nice command starts the TOP command or process with nice value of -3.

renice command in Linux

"renice" command is used to change priorities of already running processes. In the above nice example we started process TOP with -3 priority. Let’s change that priority using renice.

renice -5 -p 91524

Command syntax:

renice -n -p - PID

pgrep command

In Linux each process is assigned a process ID, or PID. This is how the operating system identifies and keeps track of processes.

"pgrep" is the useful command to get list of PID’s for a particular service or program.

For example: If you want to see list of all PID’s of apache web service or httpd.

pgrep httpd

You will get output with PID’s of httpd service.

kill command

Every process in Linux responds to signals. Signals are commands or instructions to processes to terminate or modify their behavior.

The default way to send signals to processes is through Kill command.

The very common kill command syntax are:

kill <pid>

This sends the TERM signal to the process. The TERM signal tells the process to please terminate. This allows the program to perform clean-up operations and exit smoothly.

If the program is misbehaving and does not exit when given the TERM signal, we can escalate the signal by passing the KILL signal:

kill -9 <pid>

You can send “-KILL” instead of "-9"

killall -9 - httpd - all instances having the same process name

Here are the SIGNAL codes used by Kill command.

 Signal     Value     Action   Comment
SIGHUP        1       Term    Hangup detected on controlling terminal
or death of controlling process
SIGINT        2       Term    Interrupt from keyboard
SIGQUIT       3       Core    Quit from keyboard
SIGILL        4       Core    Illegal Instruction
SIGABRT       6       Core    Abort signal from abort
SIGFPE        8       Core    Floating-point exception
SIGKILL       9       Term    Kill signal
SIGSEGV      11       Core    Invalid memory reference
SIGPIPE      13       Term    Broken pipe: write to pipe with no
readers;
SIGALRM      14       Term    Timer signal from alarm
SIGTERM      15       Term    Termination signal
SIGUSR1   30,10,16    Term    User-defined signal 1
SIGUSR2   31,12,17    Term    User-defined signal 2
SIGCHLD   20,17,18    Ign     Child stopped or terminated
SIGCONT   19,18,25    Cont    Continue if stopped
SIGSTOP   17,19,23    Stop    Stop process
SIGTSTP   18,20,24    Stop    Stop typed at terminal
SIGTTIN   21,21,26    Stop    Terminal input for background process
SIGTTOU   22,22,27    Stop    Terminal output for background process

If you don’t know the process ID, you can use pkill command which works by using process name instead of PID’s.

pkill -9 firefox

The above command will kill firefox program by sending TERM signal.

Conclusion

Linux process management is an essential task for each and every Linux administrator. All the above tools allows you to manage system resources efficiently. You can dig deep by using all the options available with each command.

Initially, It could be a little intimidating. Once you get to know it, it’s not that hard.

All the above mentioned process management commands should work fine in all Linux flavors like CentOS, RedHat, Ubuntu and Unix systems.

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Create a website in 3 simple steps

Choose a website template, add features, then customise! - Free Online Website builder.