Assuming the following dummy command in bash:
### dummy long_operation_cmd function, for easy reproduction:
function long_operation_cmd() {
echo "operation 1"
sleep 5
echo "operation 2"
}
I am currently running a bash script in the following way:
{
long_operation_cmd
echo '===ALL DONE==='
} > /tmp/logfile.tmp &
tail -f /tmp/logfile.tmp | sed '/^===ALL DONE===$/ q' \
&& rm /tmp/logfile.tmp
This allows me run the long operation safely in the background, track the output, and make sure Ctrl+C doesn't break the execution.
Most importantly, when the operation is done, sed
gets me out of the follow mode.
I want to replace tail -f
with less +F
, and this is what I came up with:
{
long_operation_cmd
echo '===ALL DONE==='
} > /tmp/logfile.tmp &
less +F -- /tmp/logfile.tmp \
&& rm /tmp/logfile.tmp
However, I can't figure a way to break out of the follow mode automatically when the ===ALL DONE===
pattern is reached (or when the long_operation_cmd
operation is done).
Any suggestions on solving this?