Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Using the stop script at system restart

Featured Replies

Ever since v6 I've constantly had issues with the array not dismounting. Tried troubleshooting but the reality is I just wanted it to stop cleanly.

 

So I developed a userscript that works fine. Except the "run at array stop" part is too late and hence I need to run it manually, then stop array/restart etc.

 

I had heard mention of the stop script and have used this to successfully put basic comments to syslog so that appears to work. But my script as it stands doesn't work.

 

Is this actually possible and the script needs modifying?

 

The script is as below. Can it be made to run as I require?

 

#!/bin/bash

# Redirect output to both console and log file
exec > >(tee -a /mnt/user/temp/logs/StopArrayCleanly.log) 2>&1

# Logging function to include timestamps
log() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') - $*"
}

log "Starting array shutdown script..."

# 1. Unmount all SMB (CIFS) shares mounted on this server
log "Unmounting all SMB (CIFS) shares mounted on this server..."
mount | grep -i cifs | awk '{print $3}' | while read -r share; do
  if [ -n "$share" ]; then
    log "Unmounting $share"
    umount "$share"
    # Check if unmount was successful
    if mountpoint -q "$share"; then
      log "Failed to unmount $share. Forcing unmount..."
      umount -l "$share"
    fi
  fi
done

# 2. Stop all Docker containers
log "Stopping all Docker containers..."
docker ps -q | xargs -r docker stop

# 3. Stop VMs
log "Stopping all VMs..."
virsh list --name | while read -r vm; do
  if [ -n "$vm" ]; then
    log "Shutting down VM: $vm"
    virsh shutdown "$vm"
    # Initialize countdown
    max_wait=150        # Maximum wait time in seconds
    interval=10         # Interval between checks in seconds
    elapsed=0
    # Loop to check VM status
    while [ $elapsed -lt $max_wait ]; do
      sleep $interval
      elapsed=$((elapsed + interval))
      log "Checking if VM '$vm' has shut down... (Elapsed: ${elapsed}s)"
      if ! virsh list --name --state-running | grep -qw "^${vm}$"; then
        log "VM '$vm' has shut down gracefully."
        break
      fi
    done
    # After maximum wait time, forcefully destroy the VM if it's still running
    if virsh list --name --state-running | grep -qw "^${vm}$"; then
      log "VM '$vm' did not shut down within ${max_wait} seconds. Forcing shutdown..."
      virsh destroy "$vm"
      # Optional: Confirm if the destroy was successful
      sleep 5
      if ! virsh list --name --state-running | grep -qw "^${vm}$"; then
        log "VM '$vm' has been forcefully terminated."
      else
        log "Failed to forcefully terminate VM '$vm'. Manual intervention may be required."
      fi
    fi
  fi
done

# 4. Terminate SSH sessions accessing the array
log "Terminating SSH sessions accessing the array..."
# Get the PID of the current script to avoid killing itself
current_pid=$$
pids=$(lsof -t /mnt/disk* /mnt/user* 2>/dev/null | grep sshd | uniq | grep -v "^${current_pid}$")
if [ -n "$pids" ]; then
  log "Terminating SSH sessions with PIDs: $pids"
  echo "$pids" | xargs -r kill
else
  log "No SSH sessions accessing the array found."
fi

# 5. Close any open files on the array
log "Closing any open files on the array..."
pids=$(lsof -t /mnt/disk* /mnt/user* 2>/dev/null | uniq | grep -v "^${current_pid}$")
if [ -n "$pids" ]; then
  log "Terminating processes with PIDs: $pids"
  echo "$pids" | xargs -r kill
else
  log "No processes accessing the array found."
fi

# 6. Stop SMB services
log "Stopping SMB services..."
/etc/rc.d/rc.samba stop

# 7. Stop NFS services
log "Stopping NFS services..."
/etc/rc.d/rc.nfsd stop

# 8. Ensure all disk activity has ceased
log "Ensuring all disk activity has ceased..."
sleep 5

log "Script completed."
log "Safe to stop array if desired."

 

? user script plug as stopping of array ?
 

image.png.93ba06d0629bf4d1d0572dd7897a755e.png

 

its not a good idea to try and run things at shutdown due to the sigterm and process the machine will just power off and not fully run the script where every it is.
Unriad has a 90 second grace perid that its does in killing the array. This is done to assist with proper array disconnects and parity.
you should not need to run a script to PID kill task and others in the above.

 

  • Author
18 minutes ago, bmartino1 said:

? user script plug as stopping of array ?
 

image.png.93ba06d0629bf4d1d0572dd7897a755e.png

 

its not a good idea to try and run things at shutdown due to the sigterm and process the machine will just power off and not fully run the script where every it is.
Unriad has a 90 second grace perid that its does in killing the array. This is done to assist with proper array disconnects and parity.
you should not need to run a script to PID kill task and others in the above.

 

I have my shutdown time set to 600 as I have some VMs etc. The script shuts them down properly.

 

Running as a userscript at stopping of array doesn't work. It starts too late that I can see. I asked the dev for assistance but I'm guessing he's put it in the "too hard basket" as he hasn't replied to my last few messages.

 

And yes I shouldn't need a script. And yet I do. I've uploaded my syslog asking how to fix and got no where. Always seems to be something different. Yesterday was this:

 

2025-03-04 14:26:35    User.Notice    192.168.100.78    Mar  4 14:26:35 DavoUnraid root: cannot unmount '/mnt/disk5/zpool-backup/appcache_appdata/Plex-Media-Server': pool or dataset is busy

 

Eventually it rebooted dirty.

 

My script does what I believe Unraid should do by itself - ie shutdown/stop everything that could cause a dirty restart.

 

So with that behind are you able to assist with how to run it automatically at shutdown. The present manual running the script works but if I need to do a restart it doesn't run and the restart will 99% of the time be dirty.

Edited by Davo67

its not to hard. alot of what this script does is done at shutdown by unraid alreeady.

the script when called due to shutdown where just stop and break some where while running as sigterms and shutdown have a priority to kill.

Again. as unraid has a 90 second wait and cool down for parity, your vm or dockers that holds up the shutdown process is the issues not trying to run a script to kill it as that has already been sent and done and unraid will force kill it duing that sigtem going off.

You better off runing the user script plugin as a pre start to hitting the power off/reboot button.

Normal server uptime and downtime would require you to shutdown the vms and dockers before hiting the button... Thats just best practice.

The isseus isn't running the script the issue is the script would also be killed and never fully run. soemtimes this can cause panix and others are other cores services and sytems were killed when trying to call and run.

its just a bad idea when your not following standards and how Unriad expects things to happen... stopping the array is called when you hit the restart/poweroff button... if you want that script to become that button then go to the user script and add a power off command at the bottom and reboot command at the bottom and use them moving forward...

User scripts runs at stopping_svcs.  At that point, VMs and Docker are already disabled.  This is event is basically right before the disks get unmounted.

 

Hopefully I didn't cause any offence by missing your questions.  On any given day I get about 100 notifications and much of them I ignore.  This thread a fair amount of time also tends to fall into that as much of the time (most?) its questions regarding making scripts etc rather than the plugin itself

  • Author
6 minutes ago, Squid said:

User scripts runs at stopping_svcs.  At that point, VMs and Docker are already disabled.  This is event is basically right before the disks get unmounted.

 

Hopefully I didn't cause any offence by missing your questions.  On any given day I get about 100 notifications and much of them I ignore.  This thread a fair amount of time also tends to fall into that as much of the time (most?) its questions regarding making scripts etc rather than the plugin itself

Thanks, it is as I suspected and hence explains why it doesn't work as expected. Thanks for getting back to me with confirmation.

  • Author
34 minutes ago, bmartino1 said:

its just a bad idea when your not following standards and how Unriad expects things to happen... stopping the array is called when you hit the restart/poweroff button... if you want that script to become that button then go to the user script and add a power off command at the bottom and reboot command at the bottom and use them moving forward...

So the suggestion would be to modify/duplicate the script so I have:

* stop array - runs the script and then stops the array
* restart server - runs the script then restarts the server

* shutdown server - runs the script and then shuts down the server

 

I did actually having it stop the array but then restarting it would not be mounted. But there is still a place and numbers 2 and 3 are a good idea.

5 hours ago, Davo67 said:

So the suggestion would be to modify/duplicate the script so I have:

* stop array - runs the script and then stops the array
* restart server - runs the script then restarts the server

* shutdown server - runs the script and then shuts down the server

 

I did actually having it stop the array but then restarting it would not be mounted. But there is still a place and numbers 2 and 3 are a good idea.


correct.

github webui for unraid emhttp php site.

https://github.com/unraid/webgui/blob/3dba0de2cac4ddbc62511b212282f874611780c3/emhttp/plugins/dynamix.vm.manager/templates/Custom.form.php#L1753


I believe the shutdown script and commands are located in the rc.6 as parts of the mutiple emhttp php web code run theses commands when you hit the poweroff / reboot button...
https://github.com/unraid/webgui/blob/3dba0de2cac4ddbc62511b212282f874611780c3/etc/rc.d/rc.6#L278

it would be better to make a user script to do the function you want it too.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.