#!/bin/sh JATWIS_MODE="" JATWIS_LOCKFILE="" JATWIS_TIMEOUT=10000 JATWIS_FREQUENCY=600 JATWIS_FIRST_RUN=1 JATWIS_SCRIPT_LOCATION="" JATWIS_URL="" function poll_default() { echo "error: no poll handler for \"$JATWIS_MODE\", exiting..." exit 1 } JATWIS_POLL_FN=poll_default function usage() { cat << EOF Usage: jatwisd [options] --help display (this) help message -h, --https poll url using https -s, --sftp poll url using sftp -e, --exec script to run if the url is reachable -t, --timeout timeout period (in milliseconds) default: 10 seconds (10000) -f, --freq, --frequency polling frequency (in seconds) default: every 10 minutes (600) -l, --lockfile set lockfile location and enable lockfile checking -p, --no-protection disable protection agains endpoint success on first run See jatwisd(1) for more info EOF } function check_ready() { if [ -z "$JATWIS_URL" ]; then echo "error: no endpoint to poll, exiting..." exit 1 elif [ -z "$JATWIS_SCRIPT_LOCATION" ]; then echo "error: no script or executable specified, exiting..." exit 1 fi if [ ! -f "$JATWIS_SCRIPT_LOCATION" ]; then if [ ! -x "$JATWIS_SCRIPT_LOCATION" ]; then echo "error: script or executable not executable, exiting..." exit 1 fi echo "error: script or executable not found, exiting..." exit 1 fi if [ "$JATWIS_MODE" == "HTTPS" ] && [[ "$JATWIS_URL" != https://* ]]; then echo "error: https url without https:// protocol, exiting..." exit 1 fi } function trigger() { if [ $JATWIS_FIRST_RUN -eq 1 ]; then echo "error: poll succeeded on first run, exiting..." exit 1 fi sh "$JATWIS_SCRIPT_LOCATION" } function poll_https() { curl -s -o /dev/null -w "%{http_code}" "$JATWIS_URL" trigger } # parse args while [ "$1" != "" ]; do case $1 in --help) usage exit ;; -h|--https) JATWIS_URL="$2" JATWIS_MODE="HTTPS" JATWIS_POLL_FN=poll_https shift ;; -s|--sftp) JATWIS_URL="$2" JATWIS_MODE="SFTP" shift ;; -e|--exec) JATWIS_SCRIPT_LOCATION="$2" shift ;; -t|--timeout) JATWIS_TIMEOUT="$2" shift ;; -f|--freq|--frequency) JATWIS_FREQUENCY="$2" shift ;; -l|--lockfile) JATWIS_LOCKFILE="$2" shift ;; -p|--no-protection) JATWIS_FIRST_RUN=0 ;; *) echo "unknown parameter \"$PARAM\"" usage exit 1 ;; esac shift done check_ready while :; do $JATWIS_POLL_FN JATWIS_FIRST_RUN=0 sleep "$JATWIS_FREQUENCY" done