Skip to content

Instantly share code, notes, and snippets.

@bpo
Created January 2, 2012 23:28
Show Gist options
  • Save bpo/1552606 to your computer and use it in GitHub Desktop.
Save bpo/1552606 to your computer and use it in GitHub Desktop.
keepalive
#! /bin/bash
#
# # keepalive #
#
# Keeps your processes running - handy quick-restart for services.
#
# USAGE:
#
# Run a server with:
#
# keepalive my_server [ARGS]
#
# When 'my_server' exits, keepalive will restart it. If the INT signal is
# received (e.g. if you press ctrl-c), keepalive will give you a brief window to
# send it again if you really want to quit, before starting the server back up.
#
SERVER_CMD=$@
INTERRUPT_DELAY=2 # seconds
CRASH_DELAY=1 # seconds
interrupted() {
echo "Interrupt received. Press ctrl-c again to quit"
trap - INT
sleep $INTERRUPT_DELAY
trap interrupted INT
}
run() {
trap interrupted INT
until $SERVER_CMD ; do
echo "Server '$SERVER_CMD' crashed with exit code $?. Respawning.." >&2
sleep $CRASH_DELAY
done
trap - INT
}
run $SERVER_CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment