Running a Script After Shutting Down a VM


Go to solution Solved by SimonF,

Recommended Posts

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!

 

Link to comment
  • 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
Link to comment

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!

Link to comment
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

Link to comment

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

 

Link to comment

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.