Back to the main page

Find why filesystem is busy

Sometime a filesystem has to be un-mounted and it cannot, because it's busy. This script shows what processes are using it. And once you know this, you can probably kill processes, if it's okay, un-mount the filesystem.

#!/bin/sh
#set -x

programname=`/usr/bin/basename $0`
usage() {
        echo "Usage: ${programname} filesystem" ; exit 1
}

# --- check if there is argument (filesystem)
[ $# != 1 ] && usage

# -- also check if argument is filesystem, maybe it's just directory
argument=`df $1 | awk '{print $1}'`
if [ ${argument} != $1 ]; then
        echo "Argument must be filesystem, and no trailing /" ; exit 2
fi

# find number of processes using filesystem
number_of_processes=`fuser -cu $1 2>/dev/null | wc -w`
# get list of those processes
list_of_processes=`fuser -cu $1 2>/dev/null`

# --- maybe filesystem is not busy
if [ ${number_of_processes} -eq 0 ]; then
        echo "The filesysetm $1 is not busy" ; exit 0
fi

echo "The filesystem $1 is busy because ${number_of_processes} process(es) are using it!"

echo "     UID   PID  PPID   C    STIME TTY         TIME CMD"
for process in ${list_of_processes}
do
        echo "`ps -fp ${process} | tail -1`"
done
exit 0


Back to the main page