restart-app (788B)
1 #!/usr/bin/env bash 2 3 # Exit if no app name provided 4 [ -z "$1" ] && exit 1 5 6 APP_NAME="$1" 7 shift # Remove app name, leaving any additional arguments 8 9 # Check if app exists in PATH 10 command -v "$APP_NAME" >/dev/null 2>&1 || exit 1 11 12 # Check if app is running and kill it 13 if pgrep -x "$APP_NAME" >/dev/null 2>&1; then 14 # Send SIGTERM (graceful shutdown) 15 pkill -x "$APP_NAME" 16 17 # Wait for up to 2 seconds for the process to terminate 18 for i in {1..20}; do 19 pgrep -x "$APP_NAME" >/dev/null 2>&1 || break 20 sleep 0.1 21 done 22 23 # Force kill if still running 24 if pgrep -x "$APP_NAME" >/dev/null 2>&1; then 25 pkill -9 -x "$APP_NAME" 26 sleep 0.1 27 fi 28 fi 29 30 # Start the app detached with any additional arguments 31 setsid -f "$APP_NAME" "$@" >/dev/null 2>&1