Skip to content

Instantly share code, notes, and snippets.

@darkcolonist
Last active October 26, 2016 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkcolonist/743c4ae6052279981085 to your computer and use it in GitHub Desktop.
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 is a comment and it will not be run!
#http://192.168.1.61/test/curlable.php
http://192.168.1.61/test/curlable.php?client=nechrons
#http://192.168.1.61/test/curlable.php?client=space
#http://192.168.1.61/test/curlable.php?client=olympus
#http://192.168.1.61/test/curlable.php?client=thanus
#http://192.168.1.61/test/curlable.php?client=jupiter
http://192.168.1.61/test/curlable.php?client=chronus
#http://192.168.1.61/test/curlable.php?client=thanatos
# @author: Christian Noel Reyes <darkcolonist@gmail.com>
# @description: WGET iterator / CRON alternative. actions.txt
# iterator, this curls all contents of actions.txt asynchronously.
# borrowed some ideas from http://reviewsignal.com/blog/2013/08/22/long-running-processes-in-php/
# @date 2016-09-14
while true
do
while IFS= read -r LINE; do
# if the line in actions.txt is commented, don't do it
if [[ $LINE == \#* ]]; then continue; fi
OUTFILE=$LINE
# replace special characters to underscore
OUTFILE="${OUTFILE//:/_}"
OUTFILE="${OUTFILE//\//_}"
OUTFILE="${OUTFILE//./_}"
OUTFILE="${OUTFILE//=/_}"
OUTFILE="${OUTFILE//\?/_}"
OUTFILE="$OUTFILE.out"
# check if this line is already processing, if it is don't do it
CURRENTLYRUNNING="$(ps aux | grep $(echo $LINE | sed "s/^\(.\)/[\1]/g"))"
if [[ ! $CURRENTLYRUNNING ]]
then
echo processing $LINE
else
echo will not run $LINE because it\'s currently running\!
continue;
fi
OLDCONTENTS=$(tail -n 10 $OUTFILE)
echo "$OLDCONTENTS" > $OUTFILE
INLINETIME=`date +'%Y-%m-%d %H:%M:%S'`
echo $(echo $INLINETIME)] $(curl -s $LINE) >> $OUTFILE &
done < actions.txt
sleep .5
done
# - 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: iterator.sh
# ... $ nohup ./iterator.sh > /dev/null &
# ... NOTE: every url specified in actions.txt will have their own out files
# ... eg. http://localhost/processes/process1.php
# ... will have an outfile: http___localhost_processes_process1_php.out
# ... NOTE: to terminate script
# ... ps auwx | grep 'bash iterator.sh'
# ... $ kill <process id>
# ... NOTE: execution errors, make sure you use UNIX line-endings (credits to Jiegonator)
# ... NOTE: sample contents of actions.txt file
# ... http://localhost/processes/process1.php
# ... http://localhost/processes/process2.php
# ... http://localhost/modules/access.php
# ... http://localhost/daemons/engine1.php
# ... http://localhost/daemons/engine2.php
# ... http://localhost/long-running/transaction.php
# ... NOTE: you can comment out a url in actions.txt (this will prevent the curl from proceeding on that url)
# ... # http://localhost/long-running/transaction.php
# ... NOTE: to run this at reboot, just add to cron:
# ... @reboot cd /home/darkcolonist/apps/iterator-v3/; bash iterator.sh > /dev/null 2>&1
@darkcolonist
Copy link
Author

image

@darkcolonist
Copy link
Author

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