LINUX: Process handling
What is a Process?
Process is kind of program or task carried out by your PC.
For e.g. $ ls -lR , ls command ( a request to list files in a directory and subdirectory in your current directory) - It is a process.
Process defined as: "A process is a program (command given by user) to perform specific Job. In Linux when you start a process, it gives a number to that process (called PID or process-id), PID starts from 0 to 65535."
What is the use of process and PID?
As you know Linux is multi-user, multitasking OS. It means you can run more than two processes simultaneously. For e.g. to find how many files do you have on your system you may give command like:
$ ls / -R | wc –l
This command will take a lot of time to search all files on your system. So you can run such command in Background or simultaneously by adding ampersand “&” at the end of the command
$ ls / -R | wc -l &
The ampersand (&) at the end of command requests shell to start process (ls / -R | wc -l) and run it in background and then takes next command immediately. We can use bg and fg command to see background processes and bring them to foreground.
Process & PID defined as:"An instance of running command is called process and the number printed by shell is called process-id (PID), this PID can be use to refer specific running process."
Shell commands for Process handling on LINUX
Following are most commonly used command(s) to handle processes:
| For this purpose | Command | Examples |
| To see currently running process | ps | $ ps |
| To stop any process by PID i.e. to kill process | kill {PID} | $ kill 1012 |
| To stop processes by name i.e. to kill process | killall {Process-name} | $ killall httpd |
| To get information about all running process | ps -ag | $ ps -ag |
| To stop all process except your shell | kill 0 | $ kill 0 |
| For background processing (With &, use to put particular command and program in background) | linux-command & | $ ls / -R | wc -l & |
| To display the owner of the processes along with the processes | ps aux | $ ps aux |
| To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command | ps ax | grep process-U-want-to see | For e.g. you want to see whether Apache web server process is running or not then give command
$ ps ax | grep httpd |
| To see currently running processes and other information like memory and CPU usage with real time updates. | top
top command. |
$ top
Note that to exit from top command press q. |
| To display a tree of processes | pstree | $ pstree |
* To run some of this command you need to be root or equivalent user. For further detail type $man command_name
NOTE: You can only kill processes which have been created or scheduled with your permission. An Administrator can kill almost 95-98% process. But some processes cannot be killed, such as VDU Process.
