Linux Process Management from Command Line

1. Log into your server via SSH: (If you're not sure how to do that, there is a SSH HowTo here)

2. Once you're logged into your server, at the command prompt type: 
 

#ps -auxw 
OR
#top
<enter>
3. Once you see the list of processes that are running on your server, look in the CPU column and see which process is using the highest amount of your CPU.

4. Once you find this process, look all the way to the left of that same line and you'll see a number that is called the PID which stands for "Process ID" (make note of the number). You want to kill this process by following these steps:

While you have top running, hit your k key. By hitting the k key, you will now see: Kill PID 823 with signal [15]:

Example:

Cpu(s): 4.0% user, 1.4% system, 0.0% nice, 94.6% idle
Mem: 1033504k total, 723376k used, 310128k free, 94012k buffers
Swap: 2000084k total, 0k used, 2000084k free, 231276k cached
PID to kill:


Now enter the PID number of the process you want to kill.

<enter>

Type y for yes.

<enter>

~~~~~~~~~~~~~~~~~~~~~~~~~~~

Another method of killing a process is as follows:

# ps aux | grep name_of_process
At the command line type: kill -9 PID_Number_Here
You can also type:
# killall -9 name_of_process which will kill any process with that name.

<enter>
The -9 will ensure "execution".
 
The ps commands discussed so far only list processes that were started from your terminal session
[root@localhost ~]# ps -af
UID        PID  PPID  C STIME TTY          TIME CMD
root      2605  2377  0 21:35 tty5     00:00:00 top
root      3140  2663  0 21:53 pts/1    00:00:00 ps -af 
 
The ps program will display all the processes running and their PID 
numbers and other information. 
 
To see a long format of the processes in your login session, use: 
[root@localhost ~]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  2663  2661  0  75   0 -  1135 wait   pts/1    00:00:00 bash
4 R     0  3724  2663  0  78   0 -  1054 -      pts/1    00:00:00 ps
 


[root@localhost ~]# ps ux
killall [process name]
killall -KILL [process name]
killall -SIGKILL [process name]
killall -9 [process name] 
kill -s KILL [PID]
kill -KILL [PID]
 
To display a long format of all the processes in the system, use the following:
[root@localhost ~]# ps -Al
Type the following ps command to display all running process:
[root@localhost ~]# ps aux | less

To see every process on the system :

# ps -A
# ps -e

To See every process except those running as root :

# ps -U root -u root -N

To See process run by user vivek :

# ps -u vivek

The top program provides a dynamic real-time view of a running system. Type the top at command prompt:

# top

Output:

Fig.01: top command: Display Linux Tasks
Fig.01: top command: Display Linux Tasks

 

display a tree of processes

$ pstree

Sample outputs:

Fig.02: pstree - Display a tree of processes
Fig.02: pstree - Display a tree of processes

Task: Print a process tree using ps

# ps -ejH
# ps axjf

Task: Get info about threads

Type the following command:
# ps -eLf
# ps axms

Task: Get security info

Type the following command:
# ps -eo euser,ruser,suser,fuser,f,comm,label
# ps axZ
# ps -eM

Task: Save Process Snapshot to a file

Type the following command:
# top -b -n1 > /tmp/process.log
Or you can email result to yourself:
# top -b -n1 | mail -s 'Process snapshot' you@example.com

Task: Lookup process

Use pgrep command. pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to screen. For example display firefox process id:
$ pgrep firefox
Sample outputs:
3356
Following command will list the process called sshd which is owned by a user called root:
$ pgrep -u root sshd

Say hello to htop and atop

htop is interactive process viewer just like top, but allows to scroll the list vertically and horizontally to see all processes and their full command lines. Tasks related to processes (killing, renicing) can be done without entering their PIDs. To install htop type command:
# apt-get install htop
or
# yum install htop
Now type the htop command at the shell prompt:
# htop
Sample outputs:
Fig.03: htop - Interactive Linux / UNIX process viewer
Fig.03: htop - Interactive Linux / UNIX process viewer (click to enlarge)

atop program

The program atop is an interactive monitor to view the load on a Linux system. It shows the occupation of the most critical hardware resources (from a performance point of view) on system level, i.e. cpu, memory, disk and network. It also shows which processes are responsible for the indicated load with respect to cpu- and memory load on process level; disk- and network load is only shown per process if a kernel patch has been installed. Type the following command to start atop:
# atop
Sample outputs:
Fig.04: Atop Command in Action
Fig.04: Atop Command in Action

 

Running the command below will list all running processes on the system.
ps aux
Now, we use the "grep" command to filter out the process we are looking for.
ps aux | grep <process_name>
Replace <process_name> with the name of the process you want to search for. The command above will list all the lines from the output of "ps aux" that contain the string specified with the "grep" command.
For example, if you want to find if Apache Web Server is running or not. You'll run the command as follows:
ps aux | grep httpd
if the output shows some lines with httpd then that means Apache Web Server (or some process with the name "httpd") is running.