Linux find process by name

Linux: How to Find Process by Name

This post will guide you how to find a process by name on Linux. How do I find a process by name under Linux operating system.

Find Process by Name

You can use PS command to find running process in your Linux. and if you want to use ps command to find process by its name, you still need to combine with grep command. Like as followss:

devops@devops-osetc:~$ ps aux | grep -i sshd root 1092 0.0 0.2 65512 2256 ? Ss Dec18 0:00 /usr/sbin/sshd -D root 18170 0.0 0.6 94904 6796 ? Ss 10:50 0:00 sshd: devops [priv] devops 18218 0.0 0.4 94904 4320 ? S 10:50 0:00 sshd: devops@pts/1 devops 18252 0.0 0.0 14224 936 pts/1 S+ 10:53 0:00 grep --color=auto -i sshd

Find Process using pidof Command

You can also use pidof command to achieve the same result of finding process by its name in Linux, just use the following command:

devops@devops-osetc:~$ pidof sshd 18218 18170 1092

From the above outputs, you should see that this command will find the process ID of a running program.

Find Process Using pgrep Command

You can also use pgrep command to look up or signal processes based on name and other attributes. just use the following command:

devops@devops-osetc:~$ pgrep sshd 1092 18170 18218

Show all Running Process

If you want to show all running process in your Linux system, you need to use ps command with aux options. type:

devops@devops-osetc:~$ ps aux | less USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.4 185248 4444 ? Ss Dec18 0:05 /sbin/init splash root 2 0.0 0.0 0 0 ? S Dec18 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S Dec18 0:06 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< Dec18 0:00 [kworker/0:0H] root 7 0.0 0.0 0 0 ? S Dec18 1:31 [rcu_sched] root 8 0.0 0.0 0 0 ? S Dec18 0:00 [rcu_bh] root 9 0.0 0.0 0 0 ? S Dec18 0:00 [migration/0] root 10 0.0 0.0 0 0 ? S< Dec18 0:00 [lru-add-drain] root 11 0.0 0.0 0 0 ? S Dec18 0:00 [watchdog/0] root 12 0.0 0.0 0 0 ? S Dec18 0:00 [cpuhp/0] root 13 0.0 0.0 0 0 ? S Dec18 0:00 [kdevtmpfs] root 14 0.0 0.0 0 0 ? S< Dec18 0:00 [netns] root 15 0.0 0.0 0 0 ? S Dec18 0:00 [khungtaskd] root 16 0.0 0.0 0 0 ? S Dec18 0:00 [oom_reaper] root 17 0.0 0.0 0 0 ? S< Dec18 0:00 [writeback] root 18 0.0 0.0 0 0 ? S Dec18 0:00 [kcompactd0] root 19 0.0 0.0 0 0 ? SN Dec18 0:00 [ksmd] root 20 0.0 0.0 0 0 ? SN Dec18 0:01 [khugepaged] root 21 0.0 0.0 0 0 ? S< Dec18 0:00 [crypto] .

Источник

How to locate and kill a process with Linux terminal

How to kill process on Linux

Every program that is running at the end of the server is able to generate one or more processes. A single server may have several users that execute many commands that will make processes. These processes may run the foreground as well as the background. If a program is running in the foreground it can occupy the terminal where the program was initially started, and other applications cannot even be started until other processes are still running the foreground. Whereas, other processes can also run in the background, in this case, the terminal in which we began started our program can add further new commands while the program is still running. Daemons are also a type of process that keeps on running continuously. They listen for requests on particular ports. They are usually initialized at system startup and just wait in the background and keep themselves in an idle state until their service is needed.

Читайте также:  Putty ssh to linux server

At times while working, our system becomes unresponsive, a process may also take a lot of system space and may even halt. So, we need to find and kill such process in Linux to proceed further. There are many ways to kill a process in Linux which will be discussed in this tutorial. A process can be killed forcefully by these commands.

I have used Ubuntu while implementing this article. However, commands can work on any Linux distribution.

Locating a Linux process

Step 1: First of all, open Terminal by clicking on Ubuntu launcher and search for Terminal.

Find Linux terminal

Step 2: Now click on the Terminal and wait for the terminal to open.

Start Linux terminal application

Step 3: Once the terminal is opened, you will have a screen like this:

Shell window is open

The two keys important here in upcoming steps are as follows:

  1. PID – It is the Process ID. Every single process has it’s a specific 5-digit number assigned to the process itself. The numbers can be repeated when they reach a maximum limit but are always unique for a process in a system.

Step 4: top – command is used as a basic command to view all of the currently running processes that are utilizing the resources of a computer device. So, we execute top- command in the terminal and the results are

Use Linux top command to locate a process

top is itself an application. It can be used to display the list of all processes and it has a new layout when it is being executed. Here is the example of it,

  • h – Is used to display a help window. The help window has all of the commands, they provide information that is quite useful.
  • space – On pressing the space, we will get to update the process table immediately. We will not be made to wait for any time interval.
  • f – to add files to display in the layout or remove to remove particular certain fields in order to stop them from being displayed.
  • q – it is used to quit the top application and also used for additionally opened window
  • l – it is used to switch the display of load average and uptime information.
  • m – it is used to switch the display of the information of memory.
  • P (Shift + p) – it is used to sort the processes by the usage of CPU.
  • s – it is used to change the delay between refreshes.
Читайте также:  Linux mint оболочка mac

Step 5: There is another command also to see the list of all currently running processes, we use “ps –A | less” command.

Linux ps command

The list of all of the currently running processes will appear. This process shows all running processes that are having their associated PIDs “process IDs”. You can further look through this list to find out the name of the process that also matches the program you actually desire to kill. The programs that run most recently are displayed at the end of the list which you want to kill from the list. For our example, we have used Firefox as a process of our interest to be killed.

Processes and their PIDs

Once you are done press “q” to exit this window.

Killing the process by ID

Once we get the PID we can use the kill command to kill a process from the information we retrieved from ps –A command.

Step 1: We use “sudo kill PID” command for this purpose. Where PID is the process ID we want to kill.

Linux kill command

If we want to kill a specific process, we see its corresponding PID and then use it to kill that running process. E.g. we want to kill “firefox” process so, we find its PID at first.

Process list

So, here the PID of firefox is 9999. We use “sudo kill PID” to kill the process of Firefox.

Kill Firefox process

How to check if the process has been killed or not?

Then we check all running processes once we kill the one of our interest, using “ps –A | less” command”. The outcome shows that we have no firefox process (PID: 9999) in the list.

Check if the process has been killed

Killing the process by name

To Kill a program using the name of its process only works for all of the executable files (i.e., all of the runnable programs) that are meant to be open at the time of execution. All of those programs that are running in the background are not capable of being killed using the process name.

We will now kill the process of our interest using the name of that process. If we want to kill a specific process, we see its corresponding PID and then use it to kill that running process. E.g. we want to kill “firefox” process so, we find its PID at first. Kill Linux process by name

So, here the process name is firefox and we use “pkill firefox” command for killing this process.

Using pkill command

How to check if the process has been killed or not?

Then we check all running processes once we kill the one of our interest, using “ps –A | less” command”. The outcome shows that we have no firefox process in the list.

Читайте также:  String to linux time

Check if pkill command has killed the process

Conclusion

Thanks for reading this article. We have seen how we can locate and then kill a process by using its process ID or name.

Источник

How do I search for a process by name without using grep?

OP just wants a shorter command I'd guess. If there would be a thing like ps -n that would answer his need. ps -n firefox is a bit shorter than ps | grep firefox . ps can already filter on pid or processes for a user id, so it's a reasonable question to filter on process name.

8 Answers 8

The pgrep command, and its sibling pkill , exists precisely for this purpose:

  • pgrep firefox will list all processes whose commands match firefox
  • pgrep -f firefox will list all processes whose entire command lines match firefox
  • pgrep -x firefox will list all processes whose commands exactly match firefox
  • . and so on.

And naturally, pgrep will exclude itself from the match, so none of the grep rituals associated with ps | grep are needed.

The other set of tools for this are the pidof and killall commands. These aren't as flexible as pgrep and pkill .

Useful if you have a long command name and/or command line args is pgrep -f foo which uses the full process name to match foo

 -C cmdlist Select by command name. This selects the processes whose executable name is given in cmdlist. -f Do full-format listing. This option can be combined with many other UNIX-style options to add additional columns. It also causes the command arguments to be printed. When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added. See the c option, the format keyword args, and the format keyword comm. 

This is the best answer but unfortunately doesn't work on OSX. The BSD ps -C flag behaves completely differently - "Change the way the CPU percentage is calculated"

You will get all the processes with names

exmple: 1747 568 568 ? 00:00:00 colord 1833 1832 1832 ? 00:00:00 gnome-keyring-d 2263 568 568 ? 00:00:00 udisksd 2311 2311 2311 ? 00:00:00 cupsd 2315 2315 2311 ? 00:00:00 dbus 

Redirect or so copy the output to a file and then open nano , press Ctrl + W and you can search for the name you want.

top allows you to search for string when you hit uppercase L ; the process will be highlighted, and use up and down arrow keys to scroll through list of processes. Similarly, htop command allows highlighting a particular process when you hit / . And \ will filter all the processes with a particular string in the name.

For those who like awk, here's an awk oneliner: ps -eF | awk '/process-name/ ' . With ps -eF process name is always in 11th column. Alternatively if you do ps -eF | awk '' | sort you get a sorted list of processes names, sorted alphabetically. Pipe it into less command just to view the long list of files easier.

Источник

Оцените статью
Adblock
detector