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.

Running a Script After Shutting Down a VM

Featured Replies

Hello Unraid community!

 

I would like to know if there is any way to run a "user script" when a VM is shutdown.

 

I have to use the GPU reset script of Spaceinvader One (https://www.youtube.com/watch?v=0uZODoPQH9c) every time I want to start a new VM. As it's mostly my parents using the VM, I would like the script to run automatically.

 

Till now, I configured Windows to send a ssh command to Unraid when it's shutting down. But I don't think it's the best way to do it.

 

Thanks in advance for your help!

 

Solved by SimonF

  • Community Expert
  • Solution
17 minutes ago, taccle said:

Hello Unraid community!

 

I would like to know if there is any way to run a "user script" when a VM is shutdown.

 

I have to use the GPU reset script of Spaceinvader One (https://www.youtube.com/watch?v=0uZODoPQH9c) every time I want to start a new VM. As it's mostly my parents using the VM, I would like the script to run automatically.

 

Till now, I configured Windows to send a ssh command to Unraid when it's shutting down. But I don't think it's the best way to do it.

 

Thanks in advance for your help!

 

Yes you can via a hooks file.

 

Dependant on version of OS you have to change either /etc/libvirt/hooks/qemu or create dir and add script in to qemu.d in hooks.

 

here is script i use for usb manager to install.

 

qemu.d you need 6.10

https://github.com/SimonFair/USB_Manager/blob/main/source/scripts/install.sh

 

Edited by SimonF

  • Author

Hi Simon!

 

Thanks! That's exactly the solution I needed!

 

To sum up if someone needs to do the same:

- I created the directory qemu.d in /etc/libvirt/hooks/

- Inside this directory I wrote the script below. It will run Reset_GPU located in user_scripts when any VM is stopping. I don't run it immediately because I want to be sure the VM is properly off.

Spoiler
#!/bin/bash

if [[ $2 == "stopped" ]]
then
        at now + 2 minute -f /boot/config/plugins/user.scripts/scripts/Reset_GPU/script
fi

- I made it executable : chmod +x GPU_Reset

 

Thanks again Simon, I didn't know about qemu and I've been struggling on this since a pretty long time!

  • Community Expert
1 hour ago, taccle said:

Hi Simon!

 

Thanks! That's exactly the solution I needed!

 

To sum up if someone needs to do the same:

- I created the directory qemu.d in /etc/libvirt/hooks/

- Inside this directory I wrote the script below. It will run Reset_GPU located in user_scripts when any VM is stopping. I don't run it immediately because I want to be sure the VM is properly off.

  Reveal hidden contents
#!/bin/bash

if [[ $2 == "stopped" ]]
then
        at now + 2 minute -f /boot/config/plugins/user.scripts/scripts/Reset_GPU/script
fi

- I made it executable : chmod +x GPU_Reset

 

Thanks again Simon, I didn't know about qemu and I've been struggling on this since a pretty long time!

Here are the status. you can use stopped or relaased without waiting.

 

https://www.libvirt.org/hooks.html#etc-libvirt-hooks-qemu

  • Author

Thanks!

 

Yes when using "stopped" and without waiting, the reset wasn't done properly. I changed it to "release" and now it's working fine!

 

Spoiler
#!/bin/bash

if [[ $2 == "release" ]]
then
        bash /boot/config/plugins/user.scripts/scripts/Reset_GPU/script
fi

 

  • 1 year later...

Thank you. I thought this was unsolvable. I didn’t expect to meet the same needs as you here.

Thank you Simon
good to known. In proxmox there were times I would have to run a hookscript to reset and powercycle the g-card for the vm at start.

was not sure how to use or port theses when needed to unraid.

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.