Is there a way to trigger some kind of event whenever a subprocess outputs a newline in Python

Viewed 18

I have a command configure.sh that works like that:

#!/bin/bash

echo "configuring things..."
sleep 5

echo "configuring things..."
sleep 5

echo "configured!"
sleep 5

echo "some run-time logs..."
sleep 5

I'm creating a python script that will run the command and it needs to do something when the output is "configured!". Thinking of using subprocess or asyncio.subprocess. In the meanwhile, it does other stuff (basically, it runs configure.sh over and over again and when the output is configured! it means that it needs to run another python script).

I've tried:

import asyncio
async def exec():
        proc = await asyncio.create_subprocess_exec('./test.sh',stdout=asyncio.subprocess.PIPE)

        outs = await proc.communicate()

.. but it waits for the command to execute, and returns the whole output instead of doing it line by line.

Is there a way to trigger some kind of event that will happen whenever a there is a newline in the output? Even better, create a promise that executes when it outputs "configured!"

Thanks in advance

1 Answers
Related