Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Shell keepalive script (zsh) (See related posts)

Disclaimer: I do not use this script on TextDrive, due to resource consumption concerns, and would not recommend anyone else doing so without express permission from TPTB.

#!/usr/bin/env zsh

# keepalive
# 
# This is keepalive, a simple zsh script (may be hackable to work for other
# shells) for keeping things running.
# 
# Usage:
# $ keepalive  [command args]
# $ keepalive ssh -N -L 8080:127.0.0.1:8080
# 
# The next three variables can be edited to adjust the action of the script

# If the command dies ${threshold} times in succession without living more
# than ${min_runtime} seconds the script will quit.
local threshold=5
local min_runtime=60

# The script will sleep ${init_sleep_time} seconds after the first failure,
# ${init_sleep_time}*2 after the second, ${init_sleep_time}*4 times after the 
# third, and so on.
local init_sleep_time=5

# ########################### #
# No need to edit below here. #
# ########################### #
local time_started time_ended 
local sleep_time=${init_sleep_time}
local times_until_fail=${threshold}

until (( times_until_fail <= 0 )); do
  time_started=$(date +%s)
  $*
  time_ended=$(date +%s)

  # reset if it's lived longer than ${min_runtime}
  if (( (${time_ended} - ${time_started}) > ${min_runtime} )); then
    times_until_fail=${threshold}
    sleep_time=${init_sleep_time}
  fi

  (( times_until_fail -= 1 ))
 
  echo
  echo "-- $(date +"%d/%m/%y %H:%M:%S"): \`$*\` died!"
  if (( times_until_fail >= 1 )); then
    echo "-- ${times_until_fail} more executions lasting less than ${min_runtime} seconds will result in job failure."
    echo "-- Sleeping ${sleep_time} seconds..."
    echo
    sleep ${sleep_time}
  fi
  
  (( sleep_time *= 2 ))
done

echo "-- Died too many times in too short a space of time. Exiting!"

You need to create an account or log in to post comments to this site.


Related Posts