Skip to content

Instantly share code, notes, and snippets.

@tuxillo
Last active December 16, 2015 21:39
Show Gist options
  • Select an option

  • Save tuxillo/5501621 to your computer and use it in GitHub Desktop.

Select an option

Save tuxillo/5501621 to your computer and use it in GitHub Desktop.
#!/bin/bash
#set -x
FPATTERN="EP5_capture"
CAPTURE_DIR="/root/temp/"
REMOTE_URI="root@localhost"
NEXT=""
DEBUG=1
debug()
{
if [ $DEBUG -ne 0 ]; then
echo $1
fi
}
getnewfile()
{
local current=$1
if [ $current -eq 0 ]; then
filename=${CAPTURE_DIR}${FPATTERN}
else
filename=${CAPTURE_DIR}${FPATTERN}${current}
fi
# The file must exist
if [ ! -f $filename ]; then
echo "File ${filename} does not exist!!!"
exit 1
fi
lsof $filename > /dev/null 2>&1
if [ $? -eq 1 ]; then
debug "${filename} IS NOT in use"
NEXT=${filename}
else
debug "${filename} IS in use"
NEXT=""
fi
}
do_the_trick()
{
debug "Compressing ${NEXT}"
bzip2 -9 ${NEXT}
if [ $? -ne 0 ]; then
return 1
fi
debug "Sending ${NEXT} to remote ${REMOTE_URI}"
# Remember you need to setup passswordless ssh login
# Also remember to add .bz2 to the end of the filename
#scp blah blah
if [ $? -ne 0 ]; then
return 1
fi
return 0
}
fileno=0
while true
do
getnewfile ${fileno}
if [ "$NEXT" = "" ]; then
sleep 30 # Maybe 120 seconds is more appropriate
else
do_the_trick
if [ $? -eq 0 ]; then
debug "File ${NEXT} has been sent, removing"
rm ${NEXT}.bz2
let fileno++
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment