0

In my script I use

winccoaID=$(ps -aux | grep -v 'color=auto' | grep -i 'WCCOAui -console' | tr -s ' ' | cut -d ' ' -f 2)
winccLogID=$(ps -aux | grep -v 'color=auto' | grep -i 'WCCOAtoolLogView'  | tr -s ' ' | cut -d ' ' -f 2)

each line looks for a certain process and assigns the pid to the value.

On cli the command works as expected. BUT in the script the commands grab another random value and adds it with a space between the 2 values. I have checked the numbers against the process list. The numbers do not relate to other process. So there is no bogus grep spoiling my fun.

Can someone tell me what is going on?

muru
  • 69,900
  • 13
  • 192
  • 292
Jan S
  • 57
  • 6
  • Because your grep alias `grep --color=auto` isn't used by the script. – muru Aug 15 '19 at 07:13
  • 2
    Have a look at `pgrep` and forget about that `ps |grep|grep|tr|cut` _forever_. There's no guarantee whatsoever that in a pipeline like `a|b|c` `a` will finish before `b` and `c` are started. –  Aug 15 '19 at 07:15
  • @muru you nailed it! Cheers mate :) – Jan S Aug 15 '19 at 07:18
  • @mosvy, I would phrase that the other way: the processes will likely all run in parallel, but there's no guarantee that the first doesn't finish before the others start. But does it matter here when ´ps` finishes? It's usually not the `ps`, but the `grep` that ends up as a spurious result. – ilkkachu Aug 15 '19 at 07:33
  • @ilkkachu for `grep` to not appear in the output of `ps -aux | grep foo` it should be that either a) `grep` has terminated before `ps` has come to read its `/proc/PID` info or b) `grep` was started after `ps` has finished reading the `/proc` dir. a) cannot be, because `grep` has to process all the output of `ps`. So that leaves b), and "`ps` finish before `grep` is started" is an acceptable approximation for it. –  Aug 16 '19 at 02:35
  • @mosvy, oh right, sorry. I don't know what I was thinking, since of course `ps` sees itself. – ilkkachu Aug 16 '19 at 05:52
  • @ikkachu to end ps before grep runs is not feasible as one-liner, right? It should then be a hand-over from one command to the next via a variable or am I missing sonething? – Jan S Aug 19 '19 at 07:00

0 Answers0