Created
March 9, 2026 13:11
-
-
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.
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
| 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