How to exclude grep from ps command outputs results

I am running the code in terminal and in script:

ps aux | grep mysqld

But it also shows grep command. Is there any way to exclude grep from ps command outputs and final result?

Are you looking for the PID? Try the pgrep command instead of ps aux | grep '....'. It looks through the currently running processes and lists the process IDs (PIDs) which match the selection criteria to screen.

pgrep mysqld

You can search for processes called nginx/php-cgi AND owned by nginx user only:

pgrep -u nginx nginx
pgrep -u nginx nginx,php-cgi

Excluding grep from ps command output

  1. Use the grep command as follows to exclude grep:
    ps aux | grep mysqld | grep -v grep
  2. Use the pgrep command to exclude everything and only display PID:
    pgrep mysqld
  3. Add square brackets around one character of the search pattern. For example, [m]ysqld:
    ps aux | grep '[m]ysqld'

Do check the following page for more info