output piped to awk is not immediately handled

450 Views Asked by At

Consider the following dummy backup script:

#!/bin/bash
echo "rsync started"
sleep 1 # rsync time
echo "rsync completed"
echo "starting upload"
sleep 5 # upload time
echo "upload completed" 

and the following minimal start script

#!/bin/bash
/path/to/backup.sh|awk '/^rsync completed/ {print "Restarting services"}'
echo "backup completed"

I would have expected the "Restarting services" output to appear after one second, but instead it appears at the end after 6 seconds, when running under Debian Wheezy.

Where is my mistake? Why isn't the awk action directly handled when the "rsync compeleted" string is written to stdout?

1

There are 1 best solutions below

1
Muhammad Umer Khan On

Use sed instead of awk.

/path/to/backup.sh|sed 's/^rsync completed/Restarting services/g'
echo "backup completed"