Failsafe gzip
If you try to gzip file that is currently opened by other process, your program will lose the handler and therefore will not be able to read/write to it. You can verify that the i-node (
ls -li) is different after gzip.
In Solaris, you can use fuser to check what processes are currently holding on to the file.
Below script is a fail-safe version to gzip file that has no process holding on to the file.
#! /bin/ksh
#
# gzip those files that no process is holding on to it
#
PATH=/usr/bin:/bin:/usr/sbin
usage()
{
echo "Usage: $0 -n <name> [-d <dir>] [-b] [-e] [-[1-9]]"
echo " -n <name>: part of the file name"
echo " -d <dir> : directory to search, default to current"
echo " -b : begins with the <name>"
echo " -e : ends with the <name>"
echo " -1 .. -9 : gzip compression flag, default to -6"
}
set -- `getopt n:d:be123456789 $*`
if [ $? != 0 ]; then
usage
exit 1
fi
finddir="."
gzip="-6"
for i in $*
do
case $i in
-n)
name=$2
findname="*${name}*"
shift 2
;;
-d)
finddir=$2
shift 2
;;
-b)
findname="${name}*"
shift
;;
-e)
findname="*${name}"
shift
;;
-[1-9])
gzip="$i"
shift
;;
esac
done
#
# checking
#
if [ -z $findname ]; then
usage
exit 2
fi
if [ ! -d $finddir ]; then
echo "Error. $finddir does not exist"
exit 3
fi
find $finddir -name "$findname" -type f 2>/dev/null | while read f
do
pids=`fuser -u $f 2>/dev/null`
if [ -z $pids ]; then
echo "Running $gzip $f ... \c"
gzip $gzip "$f"
echo OK
fi
done
Labels: shell script, Solaris


0 Comments:
Post a Comment
<< Home