Skip to content

Commit

Permalink
Bugfixes to fgbg; general improvments to *.bash
Browse files Browse the repository at this point in the history
  • Loading branch information
mthvedt committed Sep 9, 2012
1 parent 0c0d53a commit 27b6cd2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
14 changes: 6 additions & 8 deletions bin/fgbg.bash
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,15 @@ trap "rm -f $OUTFILE" EXIT
# Executes bg command, followed by echoing done mark, to our sentinel file
echo "$BG_COMMAND"
{
$BG_COMMAND 2>&1 | tee $OUTFILE;
echo $DONE_MARK >> $OUTFILE;
$BG_COMMAND 2>&1 | tee $OUTFILE
echo $DONE_MARK >> $OUTFILE
} &
BG_TASK=$!
trap "killtree.bash $BG_TASK" EXIT
trap "killtree.bash $!; echo done" EXIT

# wait to start
BG_RESULT="unknown"
while true; do
if [[ $BG_RESULT != "unknown" ]]; then
break
fi
[[ $BG_RESULT != "unknown" ]] && break
sleep 1
# look for the lines that tell us what happened
while read line; do
Expand All @@ -65,5 +62,6 @@ done
# punch line
echo "$FG_COMMAND"
$FG_COMMAND
trap "echo done" EXIT

# after command completes
exit 0
5 changes: 5 additions & 0 deletions bin/killtree.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
# Should also work on POSIX.
# The first arg is the pid of the tree to kill. The second arg is the kill signal
# (term is default).
# If process doesn't exist, terminates with status 0.

killtree() {
local _pid=$1
[[ ! $(ps -o "pid=" | grep -x $_pid) ]] && return 0
local _sig=${2-TERM}

# Snag the child and killtree it
local _regex="[ ]*([0-9]+)[ ]+${_pid}"
for _child in $(ps ax -o "pid= ppid=" | grep -E "${_regex}" | sed -E "s/${_regex}/\1/g"); do
killtree ${_child} ${_sig}
done

kill -${_sig} ${_pid}
}

Expand Down
9 changes: 6 additions & 3 deletions bin/scanport.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/bin/bash

function scanport {
# Scanport: Find an open UNIX port.
# Arg 1: The environment variable to store the port.
# Arg 2: The port with which to begin scanning.

scanport() {
for PORT in $(seq $2 65000); do
echo -ne "\035" | telnet 127.0.0.1 $PORT > /dev/null 2>&1
if [ $? -eq 1 ]; then
if [[ ! $(netstat -an | grep "\([0-9]\{1,3\}\.\)\{4\}${PORT}") ]]; then
eval "$1=$PORT"
return 0
fi
Expand Down

0 comments on commit 27b6cd2

Please sign in to comment.