Managing Processes
PS
PS - This command is used list/see the processes that are running on the Linux system/server. Process is a running instance of a program. There are many commands which are used to monitor and control these processes in Linux and ps is one such command which is used to monitor them. Below are some of the examples which show their practical applications.
• ps -ef - List all the processes that are currently running, where -e is used to display all the process, -f is used to display full format listing
• ps -ef | grep ssh - List all the process which are related to ssh
• ps -f -u vagrant,postfix - List the process related users vagrant and postfix. You can use UID too to find the process related to that particular user like (#ps -f -u 500)
ps -f -p 1307 - List the process which has PID of 1307. You can list multiple process by listing multiple PIDs separated by commas in a single command
ps -f -ppid 1295 - List the process which has PPID of 1295
ps -C crond -L -o pid,pcpu,nlwp - List all threads for a particular process(crond). This is sometimes useful when a process gets hung and determine the threads running(NLWP)
ps -p 1307 -o uid,pid,etime - List the elapsed time for particular PID
ps aux --sort pmem - Sorts the highest memory consuming process at the bottom. You can further dig into that highest memory consuming PID/PPID and get the Memory percentage. You may use this data to find a memory leak. Where -v gives the components of virtual memroy
TOP
This command is much more interactive and real-time than the ps command. This also provides the percentage of resources actually consumed by the system.
- top - Opens up an interactive session which gives information about the resource usage
After the top command displays output screen, it is like an interactive session which require you to feed the commands to get the desired output as below
O - Gives you a range of options to sort according to the resources
d - Changes the auto refresh interval
k - kill a process by desired PID
SpaceTabKey - For instant refresh
top -u vagrant - List the process details for a specific user. In this case it is "vagrant"
PSTREE
This command shows the processes that are running on the system too. But it is better in a visual way than ps command. This command shows the running processes in the form of a tree. It requires no root privileges to run this command.
pstree - Gives you the process tree
pstree 3039 - List a process based on the PID
pstree root - Displays process tree for the user "root"
pstree -a vagrant - Display the command line arguments associated with a particular process for particular user
pstree -np - Displays the process in sorted way according to PID
pstree -h - Highlights the current process and its ancestors
FREE
This command gives us the total amount of Free, Used Physical memory and Swap memory of the system. It also gives us the information about the Buffers used by the Kernel.
free -m - Displays the amount of memory in MegaBytes. Amount of memory can also be seen in different units of Data. Following are the options
- -b for bytes
- -k for kilobytes
- -m for megabytes
- -g for gigabytes
- --tera for terrabytes
free -ms 5 - Displays the amount of memory in MegaBytes continuously every 5 seconds. "-s" is used in the command to achieve this cycle
free -t - It will display an extra line showing the column totals
UPTIME
This command gives you a one line display of current time, for how long the system is up, how users are logged on, system load averages
- uptime - Displays the uptime and average load
KILL
This command is used to send Terminate, Stop, Trap, Interrupt etc., signals to the process.
kill -l - Displays the list of signal numbers that you can choose from
kill
- Generates SIGTERM signal requesting process to terminate kill -9
- Generates SIGKILL signal for process to terminate immediately or forcefully.You can kill multiple PIDs in the following way (kill -9 1234 4356 234) where 1234, 4356, 234 are distinct processes Kill -9
can be fed to the system in multiple ways like below 1. kill -s SIGKILL <PID> where SIGKILL is the signal name 2. kill -s SIGKILL <PID> where SIGKILL is the signal name 3. kill -s 9 <PID> where 9 is the signal number
NOTE :- Signal number can be determined by using the above mentioned command kill -l. Signal name can be found out by the same command too. The shorthand notation of the signal name can be found by the command kill -l signalnumber. Below is the example :-
- 9 is the signal number for SIGKILL. Getting the shorthand notation -