Kill Processes Using a Filesystem

In my home depot I share files to laptops using NFS over a VPN network. Typically the shutdown process hangs as there appears to be processes using the mounted NFS filesystem no matter whatever orders I give to the shutdown scripts. After getting tired of debugging the shutdown process I decided to write a pre-shutdown script that kills all processes with open files in the NFS fs.

The local fileserver is called “harkko”.

#!/bin/bash

function lsof_kill {
        MOUNT=$1

        for THIS_PID in $(lsof 2>/dev/null | grep -i "${MOUNT}" | awk '{print $2}')
        do
                kill -KILL ${THIS_PID}
        done
}

echo "Killing processes with open files at mounted NFS filesystems..."

# Kill all processes with open files at /mnt/harkko
lsof_kill /mnt/harkko

# Kill all processes with open files at /mnt/huge
lsof_kill /mnt/huge

# Kill all processes with open files at /mnt/canister
lsof_kill /mnt/canister

echo "...done"

echo "Executing harkko_umount"
harkko_umount

echo "Stopping harkko VPN"
/etc/init.d/openvpn stop harkko

echo "Executing poweroff"
poweroff

Leave a Reply

Your email address will not be published. Required fields are marked *

52 + = fifty three