# @author: Christian Noel Reyes <darkcolonist@gmail.com>
# @description: WGET iterator / CRON alternative
# @date: 2013-01-16
# @changelog:
# 201301161520+0800r0 file created
# 201301161535+0800r1 added conditional statement for noresponse
# 201301161545+0800r2 added comments, bugfixes, cleaning
# 201406031023+0800r3 added argument handler / OPTARG(s)
# -----------------------------
#!/bin/bash
# begin config
URL="http://example.com/";
OUTFILE="example";
OUTLIMIT=100;
TIMEOUT=300;
TRIES=1;
while getopts u:o:l:t:r: option
do
case "${option}"
in
u) URL=${OPTARG};; # http://192.168.1.18/test/index.php
o) OUTFILE=${OPTARG};; # outfile name, auto append .out
l) OUTLIMIT=${OPTARG};; # outfile limit [default: 100]
t) TIMEOUT=${OPTARG};; # wget timeout, in seconds [default: 300]
r) TRIES=${OPTARG};; # how many times to try when timeout? [default: 1]
esac
done
# end config
COUNT=1;
# begin iteration
while [ true ]; do # infinite iteration
# php /var/www/html/logger.php
INLINETIME=`date +\'%H%M%S\'`;
TODAYDATE=`date +\'%Y%m%d\'`;
RESPONSE=$(wget -t $TRIES --timeout $TIMEOUT $URL -q -O -);
OUTPUTHEAD="t$INLINETIME.p$$.i$COUNT";
if [ "$RESPONSE" = "" ]; then
OUTPUTHEAD="$OUTPUTHEAD/F";
fi
OUTPUT="$OUTPUTHEAD] $RESPONSE";
LOG=$(tail -n $OUTLIMIT $OUTFILE".out");
echo -e "$LOG\\n$OUTPUT" > "$OUTFILE.out";
echo $OUTPUT;
let COUNT=COUNT+1;
sleep 1;
done;
#end iteration
# - notes -
# my approach for crontab
# ... run this script using nohup so it will run in the background
# ... NOTE: this script will run in an infinite loop
# ... NOTE: every iteration will be freed of memory making the return garbage collection of php in effect
# ... NOTE: to run this script, say script name is: myscript.sh
# ... $ nohup ./myscript.sh -u http://example.com/test.php?p=123 -o test123 > /dev/null &
# ... NOTE: test123.sh.out will contain
# ... $ tail myscript.sh.out
# ... $> pid: <process id> | iteration: <iteration number> | response: <output>
# ... NOTE: to terminate script
# ... $ kill <process id>
# ... NOTE: execution errors, make sure you use UNIX line-endings (credits to Jiegonator)