Search all processes linux

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.

Читайте также:  Linux professional institute 101

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 and Unix ps command tutorial with examples

Tutorial on using ps, a UNIX and Linux command for reporting information on running processes. Examples of searching by user, group, executable name and killing processes.

The UNIX and Linux ps command

  • September 5, 2016
  • Updated May 24, 2023

What is the ps command in UNIX? ¶

The ps command reports information on current running processes, outputting to standard output. It is frequently used to find process identifier numbers. It supports searching for processes by user, group, process id or executable name. Related commands include pgrep that supports searching for processes and pkill that can kill processes based on a search.

How to show processes for the current shell ¶

To show the processes for the current running shell run ps . If nothing else is running this will return information on the shell process being run and the ps command that is being run.

ps PID TTY TIME CMD 5763 pts/3 00:00:00 zsh 8534 pts/3 00:00:00 ps 

The result contains four columns of information.

  • PID — the number of the process
  • TTY — the name of the console that the user is logged into
  • TIME — the amount of CPU in minutes and seconds that the process has been running
  • CMD — the name of the command that launched the process

To demonstrate that other processes will show by just running ps a task can be put into the background before running the command.

sleep 10 & ps PID TTY TIME CMD 5763 pts/3 00:00:00 zsh 10254 pts/3 00:00:00 sleep 10258 pts/3 00:00:00 ps 

How to list all processes ¶

To list all processes on a system use the -e option.

ps -e PID TTY TIME CMD 1 ? 00:00:01 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:00 ksoftirqd/0 . 

This option can be combined with the -f and -F options to provide more information on processes. The -f option offers full-format listing.

ps -f UID PID PPID C STIME TTY TIME CMD root 1 0 0 19:58 ? 00:00:01 /sbin/init root 2 0 0 19:58 ? 00:00:00 [kthreadd] root 3 2 0 19:58 ? 00:00:00 [ksoftirqd/0] . 

The -F provides extra full format information.

ps -F UID PID PPID C SZ RSS PSR STIME TTY TIME CMD root 1 0 0 13250 6460 1 19:58 ? 00:00:01 /sbin/init root 2 0 0 0 0 1 19:58 ? 00:00:00 [kthreadd] root 3 2 0 0 0 0 19:58 ? 00:00:00 [ksoftirqd/0] . 

Another commonly used syntax to achieve seeing every process on the system using BSD syntax is ps aux .

ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 53120 6368 ? Ss Sep19 0:01 /sbin/init root 2 0.0 0.0 0 0 ? S Sep19 0:00 [kthreadd] . 

How to list all processes for a user ¶

To list all processes by user use the -u option. This supports the user ID or name.

ps -u george  PID TTY TIME CMD 1053 ? 00:00:00 systemd 1062 ? 00:00:00 (sd-pam) 1074 tty1 00:00:00 zsh . 

How to list all processes for a group ¶

To list all processes by group use the -g option. This supports the group ID or name.

ps -g users  PID TTY TIME CMD  997 ? 00:00:00 login 1053 ? 00:00:00 systemd 1062 ? 00:00:00 (sd-pam) . 

How to list all processes by process number ¶

To list all processes by process number use the -p option. This selects the processes whose numbers match the list provided to the -p option.

ps -p 12608 3995  PID TTY STAT TIME COMMAND  3995 ? Ss 0:00 st 12608 pts/2 S+ 0:04 vim content/post/unix-ps.md 

How to list all processes by executable name ¶

To list all processes by executable name use the -C option. This selects the processes whose executables match the list of executables given to the -C option.

ps -C tmux  PID TTY TIME CMD 5733 pts/0 00:00:00 tmux 5735 ? 00:00:06 tmux 

How to show a process hierarchy or tree ¶

To show a process hierarchy or tree use the -H option. This outputs a process tree.

ps -eH  PID TTY TIME CMD  5735 ? 00:00:07 tmux  5736 pts/2 00:00:00 zsh 12608 pts/2 00:00:08 vim  5763 pts/3 00:00:00 zsh 17185 pts/3 00:00:00 ps 

This may also be displayed in ASCII format by using the —forest option.

ps -e --forest  PID TTY TIME CMD  5735 ? 00:00:07 tmux  5736 pts/2 00:00:00 \_ zsh 12608 pts/2 00:00:08 | \_ vim  5763 pts/3 00:00:00 \_ zsh 16952 pts/3 00:00:00 \_ ps 

How to just get the process id ¶

A common task is to find the process id of a running process. Like many things in UNIX this can be achieved in a number of ways. In the examples above the ps command can look for processes by user, group or executable name. The ps can be also be piped to grep to search for arbitrary items.

ps -ef | grep vim george 12608 5736 0 21:00 pts/2 00:00:11 vim content/post/unix-ps.md george 18324 5763 0 21:32 pts/3 00:00:00 grep vim 

On many systems the pgrep command also exists that supports a number of ways to search for a process id. This is very useful if you are just interested in process id rather than other information. To search for all processes for an executable the pgrep command can be used.

Читайте также:  Samsung smart view linux

To search by user pass the -u option.

pgrep -u george 1053 1062 1074 . 

To search by group pass the -G option.

pgrep -G users 997 1053 1062 . 

How to search for an kill a process ¶

To search for an kill a process the ps command can first be used to find the process id before using the kill command to terminate the process. In the following example ps is piped to grep and awk .

sleep 100 & [1] 21664 kill $(ps -e | grep 'sleep' | awk '') [1] + terminated sleep 100 

On some systems the pkill command is also available to accomplish this.

sleep 100 & pkill sleep [1] + terminated sleep 100 

Further reading ¶

Tags

Can you help make this article better? You can edit it here and send me a pull request.

See Also

  • Linux and Unix ping command tutorial with examples
    Aug 25, 2016
    Tutorial on using ping, a UNIX and Linux command for sending ICMP ECHO_REQUEST packets to network hosts. Examples of checking if a remote host is up and limiting to IPv4 and IPv6 requests.
  • Linux and Unix fmt command tutorial with examples
    Aug 15, 2016
    Tutorial on using fmt, a UNIX and Linux command for formatting text. Examples of formatting a file, setting the column with and formatting uniform spaces.
  • Linux and Unix comm command tutorial with examples
    Aug 10, 2016
    Tutorial on using comm, a UNIX and Linux command for comparing two sorted files line by line. Examples of showing specific comparisons and ignoring case sensitivity.

Источник

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