1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#!/bin/sh
SNIPE_OUTDIR=${SNIPE_OUTDIR:-$HOME/.snipe}
PROGNAME=`basename $0`
# setup $SNIPE_OUTDIR
if [ ! -d "$SNIPE_OUTDIR" ]; then
mkdir -p "$SNIPE_OUTDIR" || error "Cannot create $SNIPE_OUTDIR. Change value of \$SNIPE_OUTDIR and try again."
fi
[ -r "$SNIPE_OUTDIR" -a -w "$SNIPE_OUTDIR" -a -x "$SNIPE_OUTDIR" ] || warn "Permissions problem with $SNIPE_OUTDIR, should be rwx."
# avoid using Sun's crappy old awk
AWK=`which gawk 2>/dev/null`
AWK=${AWK:-`which nawk`}
AWK=${AWK:-`which awk`}
# need ksh or bash for subshell pid hack
KSH=`which ksh 2>/dev/null`
KSH=${KSH:-`which bash`}
usage() {
[ $# -gt 0 ] && echo "$PROGNAME: $@" 1>&2
echo "usage:" 1>&2
echo "list running auctions: $PROGNAME" 1>&2
echo "start auctions: $PROGNAME auctionfile [...]" 1>&2
echo "get status of auctions: $PROGNAME (-i | -I num) auctionfile_or_pid [...]" 1>&2
echo "kill auctions: $PROGNAME -k auctionfile_or_pid [...]" 1>&2
echo "kill all auctions: $PROGNAME -k" 1>&2
exit 2
}
# error exitcode message
error() {
exitcode="$1"
shift
echo "$PROGNAME: $@" 1>&2
exit "$exitcode"
}
# warn message
warn() {
echo "$PROGNAME: $@" 1>&2
}
active() {
if grepSnipes >/dev/null 2>&1; then
echo "Currently active snipes:"
grepSnipes
else
echo "No snipes currently active."
false
fi
}
grepSnipes() {
ps -ef | grep "^$USER[ \\t].*[ /]esniper "
}
# killSnipes <auctionfile_or_pid>
killSnipes() {
PID=`grepSnipes | awk '$NF == "'"$1"'" {print $2; exit}'`
if [ "$PID" = "" ]; then
PID=`grepSnipes | awk '$2 == "'"$1"'" {print $2; exit}'`
if [ "$PID" = "" ]; then
echo "unknown auction file or pid: $1"
return
fi
fi
kill "$PID"
}
# info <auctionfile_or_pid>
info() {
outfile="$SNIPE_OUTDIR/$1.txt"
if [ ! -f "$outfile" ]; then
PID=`grepSnipes | \
$AWK '$0 ~ " esniper ([^ ]* )*'"$1"'$" {print $2; exit}'`
if [ "$PID" = "" ]; then
outfile=`ls -t "$SNIPE_OUTDIR"/*.txt | $AWK '{
fn = $0
ret = getline <fn
close(fn)
if (ret > 0 && $0 == "# esniper '"$1"'") {
print fn
exit
}
}'`
else
outfile="$SNIPE_OUTDIR/$PID.txt"
fi
if [ ! -f "$outfile" ]; then
echo "Cannot find info on $1"
return
fi
fi
echo "Info on $1:"
case "$LINES" in
all) cat "$outfile";;
*) tail -"$LINES" "$outfile";;
esac
}
OP=run
LINES=
while getopts "iI:k" opt; do
case "$opt" in
i) OP=info; LINES=all;;
I) OP=info; LINES="$OPTARG";
echo "$LINES" | grep '^[0-9][0-9]*$' >/dev/null 2>&1 || \
usage "Argument to -I must be numeric";;
k) OP=kill;;
\?) usage;;
esac
done
shift `expr $OPTIND - 1`
if [ $# -eq 0 ]; then
case "$OP" in
run | info)
active
;;
kill)
if active; then
echo killing...
grepSnipes | awk '{print $2}' | xargs kill
# wait until processes die (hopefully)
for attempts in 1 2 3 4 5; do
grepSnipes >/dev/null 2>&1 || break
sleep 1
done
# what's left?
active
fi
;;
esac
else
case "$OP" in
run)
for i in "$@"; do
if [ ! -f "$i" ]; then
warn "Auction file $i not found"
continue
fi
(PID="`"$KSH" -c 'echo $PPID'`"; \
echo "$PID" >"$SNIPE_OUTDIR/lastpid"; \
echo "# esniper $i" >"$SNIPE_OUTDIR/$PID.txt"; \
exec esniper "$i" >>"$SNIPE_OUTDIR/$PID.txt" 2>&1) &
done
;;
info)
info "$1"
shift
for i in "$@"; do
echo
info "$i"
done
;;
kill)
for i in "$@"; do
killSnipes "$i"
done
;;
esac
fi
|