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!)

About this user

Nick Stenning

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

Shell keepalive script (zsh)

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!"

Bash function to copy SSH DSA public key to a new server

Or, "How to login over SSH without a password".

First of all, you'll need to have generated an SSH public DSA key using the following commands:

cd ~ && ssh-keygen -t dsa


Then, you can add the following bash function to your .bashrc (or similar) and just type 'scpdsa user@hostname' to add your public key to your 'authorized_keys' file on the server.

function scpdsa {
  if [[ -z "$1" ]]; then
    echo "!! You need to enter a hostname in order to send your public key !!" 
  else
    echo "* Copying SSH public key to server..." 
    ssh ${1} "mkdir -p ~/.ssh && cat - >> ~/.ssh/authorized_keys" < ~/.ssh/id_dsa.pub
    echo "* All done!"
  fi
}


-- updated to use simpler and quicker method.
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed