Process management Tips

Its necessary for a system admin to know about the process states and its management .

In Linux every activity is controlled by certain process. or processes are programs which are in execution.
A process consists of resources such as open file, internal kernel data ,an address space and its executing program code etc.
Each process will run with its own process id and all the running process are stored as files inside a virtual directory /proc

Process states :-

1. Running  : This is a state where a process is either in running or ready to run.
2. Interruptible  : This state is a blocked state of a process which awaits for an event or a signal from another process
3. Uninterruptible: It is also a blocked state. The process is forced to halt for certain condition that a hardware status is waited and a signal could not be handled.
4. Stopped : Once the process is completed, this state occurs. This process can be restarted
5. Zombie  : In this state, the process will be terminated and the information will still be available in the process table.

Zombie is a process state when the child dies before the parent process. In this case the structural information of the process is still in the process table. Since this process is not alive, it cannot react to signals. Zombie state can finish when the parent dies. All resources of the zombie state process are cleared by the kernel

System Calls used for Process management :-

Fork()         :- Used to create a new process
Exec()         :- Execute a new program
Wait()          :- wait until the process finishes execution
Exit()           :- Exit from the process
Getpid()      :- get the unique process id of the process
Getppid()   :- get the parent process unique id
Nice()          :- to bias the existing property of process

Commands used to manage process :-

1) PS
ps is used to list all the process which are presently running in the server

# ps aux

( ps is the one command which is not using “-” with options )
a : all process
u : with username
x : processes w/o controlling ttys
A/ e : also will list all the process

# ps aux | grep java
the above command will list all the java process

# ps aux –forest

This command will output a process tree structure

2) Pgrep

pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria

# pgrep -u root sshd
will only list the processes called sshd AND owned by root

# pgrep java
will all the java processes

3) top
The  top  program provides a dynamic real-time view of a running system.  It can display system summary information as well as a list of tasks currently being managed by the Linux
kernel. top is just like a the ps command . Main difference between top and ps is , top will give dynamic results but ps will give a static output.

# top -c
May be this will be the command mostly used by a system admin in his/her servers. Option “c” will give a command mode interface also
We can sort ” top ” results like memorywise process wise etc

shift+ m :  will sort the result in memory wise
shift+ p : will sort the result in cpu usage

# top -c -u root
will give the dynamic display of process which are owned by root user.

Process Investigation

Its always better to know about the details like from where its running and which are the files opened by the specific process etc of a process . This
knowledge will help us to manage/control process running in our server.
As mentioned earlier all the running process are stored inside a virtual directory /proc . Each process will be a folder inside /proc

For example , from top I found one perl process is taking more memory resouce and its causing the server load issue. So i decided to investigate about this process.

# cat /proc/<pid from top>/environ
the above will give you a clear idea about the running process .
Or the best and efficient command to get the process details is lsof
# lsof -p  <pid from top> | more
this is my favorite command 😉

this will clearly mention from which location this file is running and which are the open files .

Process Killing
Usually we are using process killing stop some unwanted/resource using process.
There are lots of ways to kill a running process

# kill -9  <pid>
Kill the process which having the specific pid
# pkill <process name >
kill/stop the process which are running under a specific service
# fuser -k < process deamon >
example : fuser -k /usr/local/apache/bin/httpd
This will kill all the process which are running due to apache service.  You should restart the service once you have run the above.
This command is very useful while we are facing high load issues on the server. Its very quick and efficient.