document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. # @author: Christian Noel Reyes <darkcolonist@gmail.com>
  2. # @description: WGET iterator / CRON alternative
  3. # @date: 2013-01-16
  4. # @changelog:
  5. #   201301161520+0800r0 file created
  6. #   201301161535+0800r1 added conditional statement for noresponse
  7. #   201301161545+0800r2 added comments, bugfixes, cleaning
  8. #   201406031023+0800r3 added argument handler / OPTARG(s)
  9. # -----------------------------
  10. #!/bin/bash
  11. # begin config
  12. URL="http://example.com/";
  13. OUTFILE="example";
  14. OUTLIMIT=100;
  15. TIMEOUT=300;
  16. TRIES=1;
  17. while getopts u:o:l:t:r: option
  18. do
  19.     case "${option}"
  20.     in
  21.       u) URL=${OPTARG};;      # http://192.168.1.18/test/index.php
  22.       o) OUTFILE=${OPTARG};;  # outfile name, auto append .out
  23.       l) OUTLIMIT=${OPTARG};; # outfile limit [default: 100]
  24.       t) TIMEOUT=${OPTARG};;  # wget timeout, in seconds [default: 300]
  25.       r) TRIES=${OPTARG};;    # how many times to try when timeout? [default: 1]
  26.     esac
  27. done
  28. # end config
  29. COUNT=1;
  30. # begin iteration
  31. while [ true ]; do # infinite iteration
  32.   # php /var/www/html/logger.php
  33.   INLINETIME=`date +\'%H%M%S\'`;
  34.   TODAYDATE=`date +\'%Y%m%d\'`;
  35.   RESPONSE=$(wget -t $TRIES --timeout $TIMEOUT $URL -q -O -);
  36.   OUTPUTHEAD="t$INLINETIME.p$$.i$COUNT";
  37.   if [ "$RESPONSE" = "" ]; then
  38.     OUTPUTHEAD="$OUTPUTHEAD/F";
  39.   fi
  40.   OUTPUT="$OUTPUTHEAD] $RESPONSE";
  41.   LOG=$(tail -n $OUTLIMIT $OUTFILE".out");
  42.   echo -e "$LOG\\n$OUTPUT" > "$OUTFILE.out";
  43.   echo $OUTPUT;
  44.   let COUNT=COUNT+1;
  45.   sleep 1;
  46. done;
  47. #end iteration
  48.  
  49. # - notes -
  50. # my approach for crontab
  51. # ... run this script using nohup so it will run in the background
  52. # ... NOTE: this script will run in an infinite loop
  53. # ... NOTE: every iteration will be freed of memory making the return garbage collection of php in effect
  54. # ... NOTE: to run this script, say script name is: myscript.sh
  55. # ...  $ nohup ./myscript.sh -u http://example.com/test.php?p=123 -o test123 > /dev/null &
  56. # ... NOTE: test123.sh.out will contain
  57. # ...  $ tail myscript.sh.out
  58. # ...  $> pid: <process id> | iteration: <iteration number> | response: <output>
  59. # ... NOTE: to terminate script
  60. # ... $ kill <process id>
  61. # ... NOTE: execution errors, make sure you use UNIX line-endings (credits to Jiegonator)
');