Skip to content

Instantly share code, notes, and snippets.

@deads2k
Created October 28, 2024 15:40
Show Gist options
  • Save deads2k/ca34a28a93ca01e04498ec036cc910e0 to your computer and use it in GitHub Desktop.
Save deads2k/ca34a28a93ca01e04498ec036cc910e0 to your computer and use it in GitHub Desktop.
#!/bin/bash
trap 'echo "Caught interrupt! Gracefully delete 25s sleep to let another pod start..."; sleep 25; exit 0' INT
while true; do
echo "Sleeping for 1h..."
sleep 3600
done
@sanchezl
Copy link

If Bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.

Bash will interrupt wait, so one option is to background sleep and block on wait waiting for the sleep call to finish.

  sleep 3600 & wait $!

Technically, the background sleep call will still run to completion, but the exit in the trap takes care of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment