Last active
January 7, 2025 05:15
-
-
Save aofei/b96eedf4edd45012d8bdc6ae2ee1327d to your computer and use it in GitHub Desktop.
A shell script for supervising and keeping processes alive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
if [[ "$#" -lt 2 || ! -x "$2" ]]; then | |
echo "Usage: supervise log_tag program [program-argument...]" >&2 | |
exit 2 | |
fi | |
LOG_TAG="$1" | |
shift | |
set +e -o pipefail | |
while true; do | |
STARTED_AT="$(date +%s)" | |
"$@" 2>&1 | logger -t "${LOG_TAG}[$(pgrep -oP "$$")]" | |
EXIT_CODE="$?" | |
for CODE in 0 137 143; do [[ "${CODE}" -eq "${EXIT_CODE}" ]] && exit 0; done | |
[[ "$(($(date +%s)-${STARTED_AT}))" -gt 5 ]] && sleep 0.1 || sleep 5 | |
done |
Comments are disabled for this gist.