Skip to content

Instantly share code, notes, and snippets.

@MarkWalters-dev
Created February 13, 2021 04:24
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 MarkWalters-dev/938093503a501d60e1263ed983a6da95 to your computer and use it in GitHub Desktop.
Save MarkWalters-dev/938093503a501d60e1263ed983a6da95 to your computer and use it in GitHub Desktop.
A simple queue system written in bash
#!/usr/bin/env bash
# Parially copied from https://stackoverflow.com/questions/37318999/multiprocess-queue-in-bash
# Requires: nc, tee, grep, and bash
#
# Example usage:
# urlqueue -s
# urlqueue https://www.youtube.com/watch?v=J5cnM1ODU0Y
# urlqueue -q -p | while read -r URL # read until end of list
# do
# youtube-dl $URL
# done
# or
# while true # run forever, waiting for something to download
# do
# URL=$(urlqueue -q -p)
# if [ -z "$URL" ]; then
# sleep 30
# else
# youtube-dl $URL
# fi
# done
addit() {
echo "$URL"|tee -a "$LOGFILE" >> /dev/tcp/127.0.0.1/4444
exit 0
}
popit() {
TIMEOUT=$(( 3 * 10 )) # 3 seconds
(: < /dev/tcp/localhost/4444) 2>/dev/null || error "URL queue server not running."
while ! { read -r a < /dev/tcp/localhost/4445; } 2>/dev/null
do
sleep .1
DURATION=$(( DURATION + 1 ))
[ $DURATION -ge $TIMEOUT ] && error "Error: Timeout."
done
[ -z "$a" ] && error "Error: Received no data"
echo "$a"
exit 0
}
startqueserver() {
nc -k -l 4444 | while read -r a
do
echo "$a" | nc -l 4445
done
}
killqueueserver() {
# TODO: Plan on saving the pid of the subprocess to a file
# Not seeing how to get the pid of both nc processes right now
# $$, $!, $BASHPID, and cut -d' ' -f4 < /proc/self/stat aren't doing it
# Might have to move startqueserver into its own file to make it easier to kill
# Or maybe have it watch for a touched file to shutdown on its own
# Or just rewrite everything in python.
# Or look for an existing python script that works far better than mine ever could.
# For now I will be rude and killall
killall -9 nc
exit 0
}
error () {
[ -z "$SILENT" ] && echo "$1" 1>&2
exit 1
}
usage() {
printf "Usage: %s [ -q ] [ -s | -k | -p | URL ]
URL Add URL to the queue
-s Start URL queue server
-k Kill URL queue server
-p Pop 1 item off the queue
-q Quiet or silent mode\n" "$(basename "$0")"
exit 1
}
ARG="$1"
LOGHOME=$HOME/.local/share/urlqueue
mkdir -p "$LOGHOME"
LOGFILE=$LOGHOME/url.log
[ -z "$ARG" ] && usage
[[ "$ARG" == "-q" ]] && { # I know this doesn't work if you do -p -q because of its position
SILENT=1
shift
ARG=$1
}
[[ "$ARG" == "-s" ]] && {
(: < /dev/tcp/localhost/4444) 2>/dev/null && error "URL queue server already running."
startqueserver &
exit 0
}
[[ "$ARG" == "-k" ]] && killqueueserver
[[ "$ARG" == "-p" ]] && popit
URL=$ARG
grep -E "^$URL$" "$LOGFILE" >/dev/null 2>&1 || addit
error "URL previously queued."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment