0

I am running this script docker-maintenance-script.sh. I want to check if the same clone(s) of program(my script) is running on machine. So I am running a ps command inside the script which greps again docker-maintenance-script.sh.

ps -ef | grep -P "(?<!(grep))(docker\-maintenance\-script\.sh)" 

output while running the script was:

dxadmin   4497  5231  0 22:32 pts/0    00:00:00 bash -x docker-maintenance-script.sh

which is only infact this current proceess that is greping.

So to the problem -

I want to discard that entry as that is invoker process. I need ps list that only lists other docker-maintenance-script.sh process. not this process. I need a ps list that discards my current parent pid based on negative look behind usinf pid.

I tried this

ps -ef | grep -P "(?<!(grep))(?<!($$))(docker\-maintenance\-script\.sh)"

($$ returns current pid of process) in this case 4497. But that is not helping as it 'not match' only when its just right before (docker-maintenance-script.sh). I want to not match the string whenever $$ appears anywhere before (docker-maintenance-script.sh). Please Help

I tried too ps -ef | grep -P "(?<!($$.*))(docker\-maintenance\-script\.sh)" but that returns non-fixed length lookbehind grep error

Adil Saju
  • 1,101
  • 3
  • 12
  • 26
  • 2
    Try `"^(?!.*$$).*docker-maintenance-script\.sh"` – Wiktor Stribiżew Sep 05 '19 at 19:05
  • @anubhava how will that work, can please explain I'm curious – Adil Saju Sep 06 '19 at 02:24
  • It is explained in dup question, please check. – anubhava Sep 06 '19 at 03:49
  • @WiktorStribiżew Thank you very much. That kind of works. But your regex "not matches" line if $$ is anywhere in the line. I am wondering how would I do if I really wanted to discard only if $$ were before "docker-maintenance-script\.sh"? – Adil Saju Sep 07 '19 at 17:14
  • 1
    Simple, `"^(?!.*$$.*docker-maintenance-script\.sh).*docker-maintenance-script\.sh"`. But doesn't https://stackoverflow.com/questions/51178976/grepping-output-of-ps-exclude-the-word-grep work for you? – Wiktor Stribiżew Sep 07 '19 at 17:41
  • @WiktorStribiżew Thank you! In reply-you guys didn't get it, excluding grep is not my issue, excluding my current parent process (the current running script) was my problem – Adil Saju Sep 08 '19 at 03:44

0 Answers0