Linux find process with name

Linux find process by name

Procedure to find process by name on Linux

  1. Open the terminal application.
  2. Type the pidof command as follows to find PID for firefox process:
    pidof firefox
  3. Or use the ps command along with grep command as follows:
    ps aux | grep -i firefox
  4. To look up or signal processes based on name use:

pgrep firefox

Linux find process by name using pgrep command

pgrep command looks through the currently running processes and lists the process IDs which match the selection criteria to screen. All the criteria have to match. For example, will only list the processes called sshd AND owned by root user:
$ pgrep -u root sshd
Just look up pid for firefox process:
$ pgrep firefox

How to use ‘ps aux | grep command’

ps command shows information about a selection of the active processes:
$ ps aux
$ ps aux | grep -i ‘search-term’
$ ps aux | grep ‘firefox’
$ ps aux | grep ‘sshd’
OR use the following syntax instead of using egrep command in pipes:
$ ps -fC firefox
$ ps -fC chrome
The -C option asks ps command to select PIDs by command name.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Using pidof command to grab PIDs for any named program on Linux

The pidof command finds the process id’s (pids) of the named programs such as sshd, firefox and more. For example:
$ pidof sshd
$ pidof firefox
Sample outputs:

A note about top/htop command

To display Linux processes use top command or htop command:
$ top
OR
$ htop

See also

Getting more help

Read the man pages for the following command using man command:
$ man pgrep
$ man pidof
$ man ps

Читайте также:  Запуск mac os с внешнего диска

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Find a Process Name Using PID Number in Linux

In this article, we will look at how to find a process name by its process identification number (PID). Before we dive into the actual solution, let us briefly talk about how processes are created and identified by Linux.

Every time a user or the system (Linux) launches a program, the kernel will create a process. A process holds execution details of the program in memory such as its input and output data, variables and so on.

Importantly, since Linux is a multitasking operating system, it executes several programs simultaneously, and this means each process process must be identified specifically.

The kernel identifies each process using a process ID (PID), a every instance of process must have a unique PID from other processes which is assigned when the process is invoked, to avoid any execution errors.

The /proc file system stores information about currently running processes on your system, it contains directories for each process.

Use the ls command to list its contents, however, the list may be long, so employ a pipeline and the less utility to view the /proc contents in a more convenient way as below:

From the screenshot above, the numbered directories store information files about the processes in execution, where each number corresponds to a PID.

Below is the list of files for systemd process with PID 1:

You can monitor processes and their PIDs using traditional Linux commands such as ps, top and relatively new glances command plus many more as in the examples below:

Monitor Linux processes using traditional top command.

Monitor Linux Processes with top Command

Monitor Linux processes using glances, a new real-time process monitoring tool for Linux.

Glances – Real Time Linux Processes Monitoring

Find Out Process PID Number

To find out the PID of a process, you can use pidof , a simple command to print out the PID of a process:

Find Linux Process PID

Coming back to our point of focus, assuming you already know the PID of a process, you can print its name using the command form below:

  1. -p specifies the PID
  2. -o format enables a user-defined format

Find Out Process Name Using PID Number

In this section, we will see how to find out a process name using its PID number with the help of user defined format i.e comm= which means command name, same as the process name.

Find Linux Process Name

For additional usage information and options, look through the ps man page.

If you want to kill a process using its PID number, I suggest you to read Find and Kill Linux Processes Using its PID.

Thats it for the moment, if you know any other better way to find out a process name using PID, do share with us via our comment section below.

Читайте также:  Vlc media player для windows x64

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How find out which process is using a file in Linux?

I tried to remove a file in Linux using rm -rf file_name , but got the error:

How can I find out which process is using this file?

4 Answers 4

You can use the fuser command, like:

You will receive a list of processes using the file.

You can use different flags with it, in order to receive a more detailed output.

You can find more info in the fuser’s Wikipedia article, or in the man pages.

@jim’s answer is correct — fuser is what you want.

Additionally (or alternately), you can use lsof to get more information including the username, in case you need permission (without having to run an additional command) to kill the process. (THough of course, if killing the process is what you want, fuser can do that with its -k option. You can have fuser use other signals with the -s option — check the man page for details.)

For example, with a tail -F /etc/passwd running in one window:

Note that you can also use lsof to find out what processes are using particular sockets. An excellent tool to have in your arsenal.

For users without fuser :

Although we can use lsof, there is another way i.e., we can query the /proc filesystem itself which lists all open files by all process.

Sample output below:

l-wx——. 1 root root 64 Aug 15 02:56 /proc/5026/fd/4 -> /var/log/filename.log

From the output, one can use the process id in utility like ps to find program name

Источник

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

In order to search for a process you can use ps with grep .

For example to search for firefox

How to get the same answer without using grep ?

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.

7 Answers 7

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.
Читайте также:  Virtualbox для mac os установка windows

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 .

  • pidof firefox will list processes whose command is firefox

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.

Источник

How to get current process name in linux?

How can I get the process name in C? The same name, which is in /proc/$pid/status . I do not want to parse that file. Is there any programmatic way of doing this?

8 Answers 8

If you’re on using a glibc, then:

Under most Unices, __progname is also defined by the libc. The sole portable way is to use argv[0]

It’s either pointed to by the argv[0] or indeed you can read /proc/self/status . Or you can use getenv(«_») , not sure who sets that and how reliable it is.

You can use __progname . However it is not better than argv[0] as it may have portability issues. But as you do not have access to argv[0] it can work as follows:-

I often make use of following call,

Look at the value of argv[0] which was passed to main . This should be the name under which your process was invoked.

This is a version that works on macOS, FreeBSD and Linux.

If you cannot access argv[] in main(), because you are implementing a library, you can have a look at my answer on a similar question here.

It basically boils down into giving you access to argc, argv[] and envp[] outside of main(). Then you could, as others have already correctly suggested, use argv[0] to retrieve the process name.

for posterity, a version that’s more C++-ish and works also on MSVC:

Not the answer you’re looking for? Browse other questions tagged c linux or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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