How to send stdin to redis channel in realtime?

456 Views Asked by At

I want to send the stdout and stderr of terminal while I run a huge batch file. The batch file echoer.sh

echo $HOME
sleep 3
echo $HOME
sleep 3
echo $HOME
# n times

Executing in terminal

./echoer.sh | redis-cli -x publish echoer

This waits for the entire execution of echoer.sh to finish and then send the publish command. Is there a way to publish the output as soon as you receive it?

Current Output

# command
redis-cli subscribe echoer
# output below
1) "message"
2) "echoer"
3)"/home/prashant\n/home/prashant\n/home/prashant\n/home/prashant\n/home/prashant\n/home/prashant\n/home/prashant\n/home/prashant\n/home/prashant\n/home/prashant\n/home/prashant\n
2

There are 2 best solutions below

0
On BEST ANSWER

Using xargs , it sends each line to echoer channel using xargs utility.

 ./echoer.sh| xargs -n 1 redis-cli  publish echoer
1
On

Sure, read the output of your script one line at a time and then publish that line to Redis:

./echoer.sh | while read line ; do echo "$line" | redis-cli -x publish echoer ; done