1

So I am trying to create a script that will wait for a certain string in the output from the command that's starting another script.

I am running into a problem where my script will not move past this line of code

$(source path/to/script/LOOPER >> /tmp/looplogger.txt)

I have tried almost every variation I can think of for this line

ie. (./LOOPER& >> /tmp/looplogger.txt)

bash /path/to/script/LOOPER 2>1& /tmp/looplogger.txt etc.

For Some Reason I cannot get it to run in a subshell and have the rest of the script go about its day.

I am trying to run a script from another script and access it's output then parse line by line until a certain string is found

Then once that string is found my script would kill said script (which I am aware if it is sourced then then the parent script would terminate as well).

The script that is starting looper then trying to kill it-

#!/bin/bash

# deleting contents of .txt
echo "" > /tmp/looplogger.txt

#Code cannot get past this command
$(source "/usr/bin/gcti/LOOPER" >> /tmp/ifstester.txt)


while [[ $(tail -1 /tmp/looplogger.txt) != "Kill me" ]]; do
        sleep 1
echo ' in loop ' >> /tmp/looplogger.txt

done >> /tmp/looplogger.txt

echo 'Out of loop' >> looplogger.txt

#This kill command works as intended
kill -9 $(ps -ef | grep LOOPER | grep -v grep | awk '{print $2}')

echo "Looper was killed" > /tmp/looplogger.txt

I have tried using while IFS= read -r as well. for the above script. But I find it's syntax alittle confusing.

Looper Script -

./LOOPER

#!/bin/bash

# Script to test with scripts that kill & start processes
let i=0

# Infinite While Loop
while :
do
i=$((i+1))
        until [ $i -gt 10 ]
        do
                echo "I am looping :)"
                sleep 1
                ((i=i+1))
        done
echo "Kill me"
sleep 1

done

Sorry for my very wordy question.

cdraper
  • 147
  • 2
  • 7
  • 1
    How about `(./LOOPER >> /tmp/looplogger.txt &)`? This should put it in the background as desired, and the main script can just finish running. What is the result you're looking for from the script? It seems like there should be an easier way. – l3l_aze Aug 07 '20 at 04:28
  • Don't use `kill -9`. It prevents the process from cleaning-up (deleting tmp files, freeing resources, etc.). The default signal (15) should be good enough. For more details, [see here](https://unix.stackexchange.com/questions/8916/when-should-i-not-kill-9-a-process) and [here](http://stackoverflow.com/a/24902830/3030305). – John1024 Aug 07 '20 at 04:39
  • @cdraper : Aside from the fact that it does not make sense to use `source` here (`bash` would be more appropriate), your `LOOPER` script loops forever and therefore does not return. Hence the subsequent statements in the calling script won't ever be executed. Also, the `$(....)` does not make sense. It means that the output of LOOPER is supposed to be interpreted as command, and executed, but you are redirecting the stdout. – user1934428 Aug 07 '20 at 07:04
  • @l3l_aze My company uses cassandra, while this is a test script on my local VM... I have limitations when it comes to the way I can implement some things. When we run cassandra by running ./cassandra, the script doesn't terminate on it's own. There is a specific string in the command output that I need to be parsing for, so the script knows that cassandra is now running. Hence why I made these test scripts. I'm going to try your solution. Thank you for your response – cdraper Aug 08 '20 at 03:15
  • @John1024 sadly, we are restricted from using kill sig 15, otherwise I would. Thank you for your comment :) – cdraper Aug 08 '20 at 03:29
  • @user1934428 I didn't Think of that thank you. Also the `$(...)` was more of an example, to demonstrate that I tried everything. In hind sight it was very dumb for me to put that there in the first place – cdraper Aug 08 '20 at 03:34

0 Answers0