I have a script that boots up a test environment using docker-compose.
This script pipes the mixed stdout of many docker containers on stdout through less:
# This is part of a larger script with some setup and teardown.
$ docker-compose up --build | less +F -r
less shows an undesired behavior here: When hitting Ctrl+C, docker-compose receives it and shuts down itself. Desired behaviour is only to interrupt the following (+F) feature of less (like it does when viewing e.g. a large log).
What I want to achieve in the optimal case: Interrupt the following with the first Ctrl+C and quit the whole test environment on the second Ctrl+C.
I've toyed a bit around and tried the following things:
- Register a
trap 'do_exit' SIGINTthat would implement the logic above.docker-composehowever still exited upon Ctrl+C. - Use
trap '' SIGINTto catch SIGNT totally.docker-composehowever still got the Ctrl+C out of thin air.
Another observation:
This works in zsh: (trap '' SIGINT && docker-compose up --build | less +F -r) (it does not react to SIGINT at all)
The same line behaves differently in bash and is killed by SIGINT.
Here is the full (buggy) script for reference:
#!/usr/bin/env bash
service_name=xxx
for dir in ../1 ../2 ../3; do
if [ ! -d "$dir" ]; then
echo "docker compose requires $dir, please check $dir do exist in the same folder level"
exit 0
fi
done
docker-compose up --build | less +F -r
if [ ! $? -eq 0 ]; then
echo "Couldn't start service or Control-C was pressed"
echo "cleaning up"
docker-compose down
exit $?
fi
docker-compose rm --all --force
Any solution or experiences with this?
--
Edit: I've also tried the solutions here without any success:
pkill -INT lessfrom another terminal?pkill -INT lesskind of works (only kills less, but not docker-compose), but doesn't really answer the question. Preferably I want to be able to hit Ctrl-C directly in the terminal out of convinience (and I also want to understand how this works...)(trap '' INT && docker-compose up --build) | less +F -R?CTRL-Cwill stopdocker-compose, while it should only stop the paging mode ofless. Can you explain why(docker-compose up --build | less +F -r)is not interruptable inzshand but is inbash?