0

I have a script that runs by using bash -c and I want to know how many process are running with that script.

$ ps ax | grep my_script_run | grep -v grep | grep -v "bash -c"
11121 ?        S      0:00 bash -c export PATH="/opt/rbenv/bin:$PATH"; ruby my_script_run.rb
11173 ?        Rl    19:38 ruby my_script_run.rb

Somehow with the command above doesn't exclude bash -c string.

$ ps ax | grep my_script_run | grep -v grep | grep -v bash
11173 ?        Rl    19:42 ruby my_script_run.rb

This command works fine, but I want to exclude bash -c, not bash itself. Why the first command doesn't exclude bash -c? And how can I exclude it properly?

Edit

I forgot to write I'm using wc -l after the grep

closed as off-topic by cas, jimmij, slm Jun 11 '16 at 16:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – cas, jimmij, slm
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You don't need to change anything to exclude strings with spaces in - the command you've written here should work, and it does when I try it (although I don't have your ruby script). What version of grep are you using, and is it aliased to anything? – JigglyNaga Jun 11 '16 at 7:16
  • I use alias grep='grep --color=auto -i', that causes changes color of -c and therefore it doesn't match with it. Thanks! – ironsand Jun 11 '16 at 7:28
  • 2
    I also have alias grep='grep --color=auto -- the "auto" means that color codes are only sent to the terminal, not when redirected to pipe/file. – JigglyNaga Jun 11 '16 at 7:33
  • 1
    I also aliased ps with alias ps='grc ps', that was the real reason of the behavior. I only checked if the command works with another user. – ironsand Jun 11 '16 at 7:45
  • 1
    you want to keep 'bash' and exclude 'bash -c' ? – Archemar Jun 11 '16 at 8:36
1

what about

ps ax | grep -c [m]y_script_run

which should count 2 process.

Or

\ps ax  | \grep  '[m]y_script_run' | \grep -v 'bash -c' -c

where (edit)

  • -c will count line
  • [m]y_script_run is a regexp trick to grep my_script_run but not itself. (drawback is that it only work on fixed string, not in ${myvar}). Thanks to @G-Man this string should be quoted.

  • \ps and \grep unalias grep and ps (just in case)

  • I didn't know about the option grep -c, thanks. But your code excludes bash, not the bash -c. – ironsand Jun 11 '16 at 7:31
  • 1
    If you're going to use a string like [m]y_script_run as a command-line argument, you should quote it (or escape the [).  Otherwise, if you use it in the directory where the my_script_run file is, it will be subjected to pathname "expansion" by the shell, and, as a result, it will collapse to just my_script_run. – G-Man Jun 11 '16 at 10:25
-1

To avoid grep treat -c as option there are few ways where some of it are

  1. grep -v -- 'bash -c'
  2. grep -v 'bash \-c'

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