Skip to content

Instantly share code, notes, and snippets.

@danilopinotti
Created March 9, 2026 13:11
Show Gist options
  • Select an option

  • Save danilopinotti/401fe46e8ead55210723a4de93b7c745 to your computer and use it in GitHub Desktop.

Select an option

Save danilopinotti/401fe46e8ead55210723a4de93b7c745 to your computer and use it in GitHub Desktop.
A simple shell script function to keep a process running continuously, auto-restarting it if it stops or crashes.
keepalive() {
local command_args=("$@")
# Check if a command was provided
if [ -z "$1" ]; then
echo "Usage: keep_alive <command> [arguments]"
return 1
fi
# Handle manual interruption (Ctrl+C)
trap 'echo -e "\n[Interrupted by user] Terminating..."; return' INT
echo "Starting monitoring for: ${command_args[*]}"
while true; do
# Execute the command
"${command_args[@]}"
# Logic for when the process exits
echo "Process stopped at $(date). Restarting in 2 seconds..."
sleep 2
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment