0

Objective:
- script-0.sh: produces infinite lines of output.
- script-1.sh: takes a line as input, if that line contains pattern X it must kill script-0.sh.

Proposed partial solution:
- run: ./script-0.sh | ./script-1.sh PID.
- If script-1.sh detects pattern X in its input, it calls 'kill -INT PID' and thus kills script-0.sh.

Problem:
- How do I assign the correct value to variable PID?

MuadDev
  • 392
  • 3
  • 12
  • You ask for x, yet the title asks for y (for which you already have the solution). – fancyPants Aug 30 '17 at 12:38
  • @fancyPants `kill -INT` is same as clicking ctrl-c but however the title is indeed contradictory – Papershine Aug 30 '17 at 12:42
  • The title might be somewhat unclear ideed. The title indicates the general case, for which I have a solution indeed. But for this specific use case I do not have a solution... I hope this clears it up. – MuadDev Aug 30 '17 at 12:51
  • @paper1111 Control-C is slightly more general; see https://stackoverflow.com/a/8406413/1126841. – chepner Aug 30 '17 at 13:07

1 Answers1

0

What you can do is write the PID to a file and then read it from the script you want to use to kill the other script by using the echo $$ >> somefile.pid (which will output the PID and then concat it to the file) as it is detailed here https://askubuntu.com/questions/526886/stopping-a-running-script-though-another-script-file.

Agustin
  • 95
  • 2
  • 10
  • Thank you for your answer. This solution is not as "pretty" as I had hoped for, but might be effective nonetheless. I will try this out and report back. If anyone has another solution that does not involve using an extra file I would appreciate it. – MuadDev Aug 30 '17 at 12:54
  • @MuadDev The other thing you can do is pass as parameter the PID from the first script to the second as you are doing but inside the script, by calling inside the first script and getting it with `$1` using `$$`. – Agustin Aug 30 '17 at 13:01
  • of course! that might be a nice solution, thanks. – MuadDev Aug 30 '17 at 13:05
  • @MuadDev can you mark it as correct if that is the case? :D – Agustin Aug 30 '17 at 13:20
  • @MuadDev did it work? – Agustin Aug 31 '17 at 13:04