0

I'm trying to stop a long running process as soon as the file /status contains the string Success.

I tried this without success:

cat & while [ `grep -q Success /status` ]; do sleep 1; done; kill %1

cat is the long running process that needs to be stopped when /status contains Success.

Cheers

Joan
  • 4,079
  • 2
  • 28
  • 37
  • Is `cat` an example or is the real command? Also, who creates the file? – Robert Jul 16 '17 at 13:45
  • What's the use case? – johnharris85 Jul 16 '17 at 15:37
  • @Robert @johnharris85 thanks for your comments, I edited the question. `cat` is indeed the long running process that can be replaced by anything else really. – Joan Jul 16 '17 at 16:15
  • 1
    ```[ `grep -q Success /status` ]``` is always false, because you're looking at the **stdout** of `grep`, not its exit status, and the stdout of `grep -q` is (intentionally!) always empty. – Charles Duffy Jul 16 '17 at 16:28
  • BTW, use `cat & cat_pid=$!`, then later `kill "$cat_pid"` -- job control (`%1`) is only guaranteed to be available in interactive shells. – Charles Duffy Jul 16 '17 at 16:30
  • BTW -- is this file being appended to? Can it grow long? If so, continually re-`grep`ing from the beginning is very, *very* inefficient. – Charles Duffy Jul 16 '17 at 16:31
  • ...you might https://stackoverflow.com/questions/30787575/using-tail-f-on-a-log-file-with-grep-in-bash-script informative, in terms of trying to build a better implementation. – Charles Duffy Jul 16 '17 at 16:33
  • This question is not an exact duplicate as the one pointed. Here is involved a question on how terminate a process too. @CharlesDuffy – Robert Jul 16 '17 at 16:41
  • @Robert, if your point is that the question is too broad (incorporates multiple distinct scopes rather than being properly narrowed to one, as site guidelines request), I'll accept that point as valid -- but it *doesn't* argue for reopening. – Charles Duffy Jul 16 '17 at 16:49
  • @Robert, ...moreover, since one can have more than a single dupe flag now, that could be remedied by adding a question asking how to terminate a process as a second dupe target. – Charles Duffy Jul 16 '17 at 16:50
  • @Robert, ...and said remedy is performed; we're now *also* flagged as a duplicate of https://stackoverflow.com/questions/36369136/how-to-kill-a-background-process-created-in-a-script. – Charles Duffy Jul 16 '17 at 16:52
  • @CharlesDuffy, thank you for the information. The confusion started for me from the first version of this post, which lead me to think solutions docker-related. Anyway, as the question looks now it seems that you are totally right. – Robert Jul 16 '17 at 17:26

0 Answers0