How can i check for active NFS sessions (in order to run auto-shutdown script)

Elljott

Cadet
Joined
Mar 12, 2023
Messages
2
Hello everyone,

I'm using TrueNAS as a backup storage device in my private network. Since a full backup once a week takes about 4 hours (increments only 10 mins), I prefer not to keep the device running all day long to save energy. Instead, I use a Wake-on-LAN script to start the backup process when needed.

Previously, I was using SMB and had a working script that checked for "DENY_NONE" and locked files in "/usr/local/bin/smbstatus". The script ran every minute and scheduled a shutdown in 15 minutes if there were no locked files. However, if a file was locked within the 15-minute period, the shutdown was canceled to prevent data loss.

Now, I've switched to NFS for better performance, but I'm facing some difficulties in implementing a similar script. The "nfsstat" command only provides statistics, and "showmount" doesn't give any output. Also, the solution I found in a 2014 forum post regarding open files and lsof does not work due to fstab's limitations and lack of ZFS support.

I'm looking for suggestions on how to implement a similar script with NFS or any other approach that could help me achieve the same goal. Any advice or insight would be greatly appreciated.

Thank you.
 

Elljott

Cadet
Joined
Mar 12, 2023
Messages
2
For those who want to read my SMB script (my comments are German):

Code:
#!/bin/sh

# Anzahl der Minuten, nach denen der Server heruntergefahren wird, wenn keine Datei mehr gesperrt ist
SHUTDOWN_DELAY=15

# Pfad zur smbstatus-Binärdatei
SMBSTATUS_PATH=/usr/local/bin/smbstatus

# Pfad zur Shutdown-Binärdatei
SHUTDOWN_PATH=/sbin/shutdown

# Prüfe, ob eine Datei gesperrt ist
if $SMBSTATUS_PATH | grep -q "DENY_NONE"; then
    # Es ist mindestens eine Datei gesperrt
    echo "Eine oder mehrere Dateien sind noch oder wieder gesperrt. Server wird nicht heruntergefahren."

    # Prüfe, ob ein Shutdown-Prozess läuft
    if pgrep shutdown > /dev/null 2>&1; then
        # Ein Shutdown-Prozess läuft
        echo "Ein Shutdown-Prozess ist bereits im Gange. Der Prozess wird abgebrochen."
        killall -SIGTERM shutdown
    else
        # Kein Shutdown-Prozess läuft
        echo "Kein Shutdown-Prozess läuft. Es wird nichts unternommen."
    fi
else
    # Keine Datei ist gesperrt
    echo "Keine Dateien sind gesperrt. Der Server wird in $SHUTDOWN_DELAY Minuten heruntergefahren."

    # Prüfe, ob ein Shutdown-Prozess läuft
    if pgrep shutdown > /dev/null 2>&1; then
        # Ein Shutdown-Prozess läuft
        echo "Ein Shutdown-Prozess ist bereits im Gange. Es wird nichts unternommen."
    else
        # Kein Shutdown-Prozess läuft
        echo "Kein Shutdown-Prozess läuft. Der Server wird in $SHUTDOWN_DELAY Minuten heruntergefahren."
        sleep "$SHUTDOWN_DELAY"m
        $SHUTDOWN_PATH -p +$SHUTDOWN_DELAY "FreeNAS wird heruntergefahren, da seit $SHUTDOWN_DELAY Minuten keine Datei mehr via SMB gesperrt ist."
    fi
fi
 
Top