Linux search 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] .

Источник

Читайте также:  Mac windows linux icons

pgrep Command Examples

Want to look for a process and its details? The pgrep command helps you with that. Here's how to use it.

Like the grep command used to find strings from files and output, the pgrep command does the same for the processes.

In simple terms, the pgrep command will get you the PID of running processes.

So in this guide, I will walk you through various examples of how you can use the pgrep command.

Using the pgrep command on Linux

To use the pgrep command, you will have to follow the given command syntax:

Here, the pattern is where you will specify the parameters for the output.

Now, let's have a look at some examples.

Find the PID of a specific process using the pgrep command

To find the PID of a specific process, all you have to do is follow the given syntax:

pgrep -u username process_name

So let's say I want to find the process ID of ssh for the user sagar so I will be using the following command:

get the PID of specific service using the pgrep command on linux

Get the number of processes owned by the user

If you want to know the number of processes being utilized by the specific user, append the username with -c and -u flag to the pgrep command as shown:

For example, here, I looked for the number of processes owned by a user named sagar :

get the number of processes owned by user on linux using the pgrep command

And if you want to combine the processes count of multiple users, you can separate them by column:

For example, here, I combined the processes of two users: sagar and root:

get process count of multiple users on linux using the pgrep command

Search processes in a case-insensitive manner

By default, the pgrep utility operates in a case-sensitive mode so you may not be able to find the desired processes.

But you can switch to the case-insensitive mode using the -i flag:

For example, here, I looked for firefox but used all capitals and it returned the right result:

Search processes in a case insensitive manner using the pgrep command on linux

List ongoing processes with their names and PIDs

If you want to list every ongoing process with their names and PID, it can easily be done using the -l flag:

Читайте также:  Linux ubuntu снимок экрана

For example, here I listed every ongoing process relative to the user named sagar :

List ongoing processes with their names and PIDs using the pgrep command on linux

Find which command was used to start the ongoing process

The pgrep utility also allows you to see the command used to start the ongoing process by using the -a flag:

So let's say I'm looking for commands that executed ongoing processes for user root, then the following command should get my job done:

Find which command was used to start the ongoing process in linux

Find the most recently started process

If you want to find the most recent ongoing process, it can easily be found using the -n flag:

But I would recommend pairing -n with -l to have more useful output as it will add use process name too:

find the most recent ongoing process on linux using the pgrep command

Find the oldest ongoing process on Linux

In case, if you want to find the oldest ongoing process on your system, the pgrep utility can do that when executed with -o flag:

For example, if I want to find the oldest ongoing process for my user (sagar), I will be using the following command:

Find the oldest ongoing process using the pgrep command

But how about killing unnecessary processes?

Once you find out the set of unnecessary processes, you can use the various signals to kill them.

Don't know how to? Well, we have a detailed guide on how to kill processes from the terminal in Linux:

And if you are interested in learning more about signals to terminate the process, we have your back:

I hope you will find the given guide helpful and if you have any queries or suggestions, feel free to let me know in the comments.

Источник

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"

Читайте также:  Interrupt in linux kernel

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.

Источник

linux grep process by name/by id and kill example, and grep -v example

Query the name of the running process, usually requires the linux ps command, linux netstat command, linux awk command and the linux grep command.

grep process by name

grep process by port

➜ grep netstat -an | grep "\*\.8080"

grep process id and kill

➜ ps -ef | grep "tail" | grep -v grep | awk -F" " 'system("kill "$2"")'

Command interpretation:

step 1. Show the process you want to kill and exclude the grep command.

➜ ps -ef | grep "tail" | grep -v grep

step 2. Get the process pid you want to kill.

➜ grep ps -ef | grep "tail -f" | grep -v grep | awk -F" " ''

step 3. Kill the process with awk system.

➜ ps -ef | grep "tail" | grep -v grep | awk -F" " 'system("kill "$2"")'

Источник

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