Monday, October 22, 2012

Processes in Linux

Type the following ps command to display all running process:
# ps aux | less
Where,
  • -A: select all processes
  • a: select all processes on a terminal, including those of other users
  • x: select processes without controlling ttys

Task: see every process on the system

# ps -A
# ps -e

Task: See every process except those running as root

# ps -U root -u root -N

Task: See process run by user vivek

# ps -u vivek

Task: top command

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
To quit press q, for help press h.

Task: display a tree of processes

pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified, all process trees rooted at processes owned by that user are shown.
$ 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:

Linux and all other UNIX like oses comes with kill command. The command kill sends the specified signal (such as kill process) to the specified process or process group. If no signal is specified, the TERM signal is sent.

Kill process using kill command under Linux/UNIX

kill command works under both Linux and UNIX/BSD like operating systems.

Step #1: First, you need to find out process PID (process id)

Use ps command or pidof command to find out process ID (PID). Syntax:
ps aux | grep processname
pidof processname

For example if process name is lighttpd, you can use any one of the following command to obtain process ID:
# ps aux | grep lighttpdOutput:
lighttpd  3486  0.0  0.1   4248  1432 ?        S    Jul31   0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
lighttpd  3492  0.0  0.5  13752  3936 ?        Ss   Jul31   0:00 /usr/bin/php5-cg
OR use pidof command which is use to find the process ID of a running program:
# pidof lighttpdOutput:
3486

Step #2: kill process using PID (process id)

Above command tell you PID (3486) of lighttpd process. Now kill process using this PID:
# kill 3486
OR
# kill -9 3486
Where,
  • -9 is special Kill signal, which will kill the process.

killall command examples

DO NOT USE killall command on UNIX system (Linux only command). You can also use killall command. The killall command kill processes by name (no need to find PID):
# killall -9 lighttpd
Kill Firefox process:
# killall -9 firefox-bin
As I said earlier killall on UNIX system does something else. It kills all process and not just specific process. Do not use killall on UNIX system (use kill -9).

No comments:

Post a Comment