1

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?

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
muffel
  • 7,004
  • 8
  • 57
  • 98
  • 2
    I guess it's because of some buffering going on. With `gawk` you can force flushing the output buffer with `fflush()`: `awk '/^rsync completed/ {print "Restarting services"; fflush()}'`. With `mawk` I was able to make it work with `-W interactive`: `awk -W interactive '/^rsync completed/ {print "Restarting services"}'`. I don't know if there are any fully portable ways to achieve that. Wait for the `awk` experts to answer! – gniourf_gniourf Feb 13 '15 at 12:33
  • 2
    @gniourf_gniourf `fflush` flushes buffers from awk to subprocesses; I don't think it is helpful here. I cannot reproduce the problem with gawk, anyway, so I assume OP is using mawk. I was about to suggest `-W interactive` as well. – Wintermute Feb 13 '15 at 12:36
  • @Wintermute yes, that solves my problem, thank you (using `-W interactive`) – muffel Feb 13 '15 at 12:38
  • @Wintermute: all right, I don't have `gawk` here, so I couldn't try it really... that was just a (useless) guess. – gniourf_gniourf Feb 13 '15 at 12:39
  • @gniourf_gniourf thank you anyway, and your second comment was the solution anyway ;) – muffel Feb 13 '15 at 12:42
  • 2
    @muffel, if nobody provides an answer, I suggest you add an answer yourself and accept it. Q's without A's are useless here. – glenn jackman Feb 13 '15 at 14:34

1 Answers1

0

Use sed instead of awk.

/path/to/backup.sh|sed 's/^rsync completed/Restarting services/g'
echo "backup completed"
  • Works in this narrow case, but awk is a much more expressive language than sed (many awk programs don't *have* any reasonable sed equivalent), so it certainly isn't a general-purpose answer. – Charles Duffy Jan 30 '19 at 22:47