Import Re Subprocess Cmd Ps Grep Little Snitch Grep Grep

PowerShell is awesome if you haven’t used it but it’s absolutely foreign if you’re used to Unix-like Bash-like shells. For instance, there’s no grep, and ls -al won’t work (but ls will and it’ll display all files with details!). If you switch from Linux to Windows, you might be tempted to either install Cygwin or.

I use ps command to find out all running process on my Linux and Unix system. The ps command shows information about a selection of the active processes on shell. You may also pipe out ps command output through grep command to pick up desired output.

Advertisements

Example: Remove grep command while grepping using ps


Let us run a combination of ps command and grep command to find out all Perl processes:
$ ps aux | grep perl
Sample output:

In above example, I am getting the grep process itself. To ignore grep process from the output, type any one of the following command at the CLI:
$ ps aux | grep '[p]erl'
OR
$ ps aux | grep perl | grep -v grep
Sample outputs:

The above output indicate that I prevented ‘grep’ from showing up in ps results. In other words, we learned to remove grep command from ps output.

Understanding above commands

Snitch

You don’t want display grep command as the process in ps output, i.e., you want to prevent ‘grep’ from showing up in ps results.

  • In first command I used regex. It says find the character ‘p’ followed by ‘erl’ i.e. the expression ‘[p]erl’ matches only ‘perl’ not ‘[p]erl’, which is how the grep command itself is now shown in the process list.
  • The second command uses the -v option to invert the sense of matching, to select non-matching lines.

Say hello to pgrep

You can look up process based upon name to get PID. This is only useful when looking for process names and PIDs The syntax is:
pgrep process-name
pgrep -a process
pgrep -l process
pgrep -u user -a process

To find the process ID of the sshd daemon:
$ pgrep -u root sshd
1101

To list PID and full command line pass the -a to the pgrep command:
$ pgrep -a sshd
Sample outputs:

To just list PID and process name pass the -l to the pgrep command:
$ pgrep -l firefox
7981 firefox

Conclusion

You learned how to avoid grep command from showing up in ps command results under Linux, macOS, *BSD or Unix-like systems. Pretty useful for excluding grep from process list when running ps. For more information see ps and grep command man pages by typing the following commands:
man ps
man grep

Import Re Subprocess Cmd Ps Grep Little Snitch Grep Grep 2

ADVERTISEMENTS