Script to execute after VM is shutdown

daan256

New Member
Jan 29, 2023
3
0
1
Hello,

Hope everyone is well.
I have a script which i would like to run when the VM is stopped (In windows click shutdown).
The Script should execute the following commands

echo 1 > /sys/bus/pci/devices/0000\:04\:00.0/remove
echo 1 > /sys/bus/pci/devices/0000\:04\:00.1/remove

I tried hoopscript but it was quite difficult. Are there other ways of executing these commands automatically when a VM is shutdown?
 
Hi,

well, this is exactly what hookscripts are for ;) what part did you struggle with? There's an example script at /usr/share/pve-docs/examples/guest-example-hookscript.pl that should serve as a good starting point.
 
hello Leo,
Thanks for your answer. I tried and created the following script. But when I shutdown the VM in Windows the device is still linked to ProxMox.
What i intend to do with this script is release an eGPU after the VM is shutdown where it is passedthrough to, so i can turn the eGPU device physically off.

1675108609344.png
 
You're mixing perl and bash syntax here :) You never assign $phase, so it's going to be empty. The script is called with two parameters: <vmid> and <phase>, so that value should be stored in $2 accordingly. Something like
Code:
if [[ "$2" == "post-stop" ]]
should do the trick. Note that the output of "post-stop" doesn't show up in the task log itself, but you should be able to find it in the syslog after stopping your guest.
 
Last edited:
Hi Leo,
thanks for your reply. Unfortunaltely the addition I made did not do the trick. Any other ideas?

1675453438878.png
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!