Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Created April 5, 2023 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariocesar/edd3aec5eb5fc7528b7b6a3153ab2124 to your computer and use it in GitHub Desktop.
Save mariocesar/edd3aec5eb5fc7528b7b6a3153ab2124 to your computer and use it in GitHub Desktop.
A simple daemon script that I used for debugging running Docker containers in AWS ECS.

Outputs

{"name": "worker", "level": "INFO", "message": "Service start", "timestamp": "2023-04-05 14:04:48.974687279", "ip": "172.17.0.2", "pid": 7}
{"name": "worker", "level": "INFO", "message": "ping", "timestamp": "2023-04-05 14:04:48.975945036", "ip": "172.17.0.2", "pid": 7}
{"name": "worker", "level": "INFO", "message": "Received signal: SIGTERM", "timestamp": "2023-04-05 14:04:53.979013322", "ip": "172.17.0.2", "pid": 7}
{"name": "worker", "level": "INFO", "message": "Stopping", "timestamp": "2023-04-05 14:04:53.980930837", "ip": "172.17.0.2", "pid": 7}
#!/usr/bin/env bash
set -e
IP=$(hostname -I | awk '{print $1}')
SLEEP_INTERVAL=5
PID=$$
print_log() {
printf \
'{"name": "worker", "level": "%s", "message": "%s", "timestamp": "%s", "ip": "%s", "pid": %d}\n' \
"$1" "$2" \
"$(date "+%Y-%m-%d %H:%M:%S.%N")" \
"${IP}" \
"${PID}"
}
trap 'print_log "INFO" "Received signal: $1" && print_log "INFO" "Stopping"; exit' SIGINT SIGTERM
print_log "INFO" "Service start"
while true; do
print_log "INFO" "ping"
sleep "${SLEEP_INTERVAL}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment