Last active
October 26, 2016 03:13
-
-
Save darkcolonist/743c4ae6052279981085 to your computer and use it in GitHub Desktop.
BASH WGET iterator / CRON alternative per second instead of per minute for Linux, forked from https://github.com/darkcolonist/snippets/blob/master/cron%20alternative%20per%20second.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @author: Christian Noel Reyes <darkcolonist@gmail.com> | |
# @description: WGET iterator / CRON alternative, forked from https://github.com/darkcolonist/snippets/blob/master/cron%20alternative%20per%20second.txt | |
# @date: 2015-11-25 | |
# ----------------------------- | |
#!/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 | |
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) |
Author
darkcolonist
commented
Sep 14, 2016
current working version is not really performance friendly! ie., the processes overtime take more than a second instead of supposedly a second at most before the iteration is called.
just use: https://gist.github.com/darkcolonist/743c4ae6052279981085/4b8fe5fa9369a338253cfb52cc698c58b9fd16f1
otherwise use the nodejs version: https://github.com/darkcolonist/node.iterator.micro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment