Some S3 commands, when run against large buckets have tendency not to finish their execution successfully (due to various reasons, network dropout being the most common) and one has to spend a lot of time identifying such cases and re-running the commands when necessary.
Typical use-cases I run into are aws s3 rm or aws s3 sync commands which, when run against a e.g. several hundred millions or a billion files bucket, can take up to several days to finish execution.
In those cases it really pays of putting such commands in bash loop and reiterating it until the process finishes successfully.
The following script does exactly that. Command can be parametrized to accept the actual S3 command you wish to run as well as the name of the bucket (which can be extended to include arbitrary key prefix) to run against.
if [ -n "$1" ]
if [ -n "$2" ]
AWS_S3_RECURSIVE_COMMAND="aws s3 "$AWS_COMMAND" --recursive s3://"$AWS_BUCKET_NAME"/"
echo "["`date +"%T"`"] Running the command and redirecting its output to '"$AWS_COMMAND_OUTPUT"': "$AWS_S3_RECURSIVE_COMMAND
until $($AWS_S3_RECURSIVE_COMMAND >> $AWS_COMMAND_OUTPUT); do
echo "["`date +"%T"`"] Re-running the command '"$AWS_S3_RECURSIVE_COMMAND"' per previous failure: "$?" (exit code)"