UNIX treats every object in the operating system as a file. We can find the files associated with every action performed and can make use of them for different system- or process-related manipulations.
A process, also known as a task, is the running form of a program. Programs are stored on disk and processes run in memory. Processes have a parent/child relationship. A process can spawn one or more children. Multiple processes can run in parallel.
If you want to see the PID of any running process then we’ve 4 best ways through which you can easily get the PID of all running processes.
With pidof command, you simply need to mention the name of the process which lists of all PID’s associated with that process
Command: pidof apache2
With pgrep command, the syntax is same pidof but it will display all PID’s in vertical mode.
Command: pgrep apache2
With pstree command, it outputs the process list arranged in a tree-like pattern showing the parent/child relationships between processes.
So, for example if you want to kill the Apache Service, then you simply note down the parent process ID of Apache Service (5640) and kill directly with either kill, killall or pkill command.
Command: pstree -p | grep “apache2”
And last but not least, the same can also be done with the help of ps command as shown below:
Command: ps aux | grep “apache2”
Kill Command –
This command simply sends a signal to terminate one or more process IDs. You must own the process or be a privileged user. If no signal is specified, TERM is sent.
Let’s for Example if you want to kill the process of Apache then you need to mention the PID of Apache Service which we’ve already found out from first command.
Command: kill <PID>
Furthermore, you can even check the status of Apache Service whether its service has been stopped or not by typing “service apache2 status” or “pstree -p | grep apache2” (If it shows no output, then it means that service is not running)
Pkill Command –
pkill stands for process kill is a command line utility which allow users to kill or terminate process in Linux through PID or process name. Make a note, pkill will kill all processes matching the process name. It allows extended regular expression patterns and other matching criteria.
For example, if you want to kill all apache processes, then the command is:
Command: pkill -e apache
Where -e stands for echo mode.
And to verify, Re-run the pstree command once again to verify whether the process is terminated or not.
Killall Command –
The only benefit to use this killall command is that you can kill any process by just the command name. If more than one process is
running the specified command, it will kill all.
For example, if you want to kill the Apache Service, then the command is:
Command: killall -v apache2
Where -v stands for verbose mode.