Script to shutdown node when no VM active for 1 hour

Frigg

Active Member
Sep 4, 2016
63
2
28
52
Hello,

Does someone could give me a script example to shutdown a node if there hase been no vm active for one houre ?
I guess what I need is a shell wraper to test evry 15' if vm are unactive, ain if it si true for 4 time, then cron commande to shutdown the node. But I have no idéa how to do that...

Thank you for your help.
 
Hi,
how do you would define an active VM?
Only with cpu/load? But what happens with background processes like "check for updates", logrotation...
On the other side, an long-run user process with only small resources will killed by an shutdowned host?

Hmm, sounds not easy for me. Easier to live-migrate to an second server if the server load is too less and shutdown the empty server (and switch on the server again (and mirgate back), if the other server reach an load of X).

Udo
 
In my oppinion you must check 3 things at the same time:
- cpu load
- network traffic
- storage/disk activity

You can make all this checks with monit(includind the proxmox migrate if you want).
 
Assuming you have some type of home-made scale out load in mind...
The only real way you can know if a VM is active is from within it, so make a script in your VMs to shut down when idle. Then in your proxmox nodes do something like:
Code:
#!/bin/bash
qm list | grep running
if [ $? -eq 0 ]
then
    shutdown -h now
fi
If the "hour" bit is important, I can't see why...
 
  • Like
Reactions: Hollu2512
Hello,
If someone could help me debug the script above, it would be greate.
The goal is to shutdown the node if no vm is running.
qm list | grep running returns nothing. It is what we want for the condition to be filled.
The problem is that the command shutdown - h now isn't sent.
There must be a problem with the syntax of the condition, but I can't find it ?
Thanks
 
Hi,
I only said try "something like" that!

"qm list" gives a list of VMs
"|" sends the output to the next command grep
"grep running" looks for running in the text and returns 1 if not found...so the next line should probably be 1 not 0.

But I wasn't suggesting a final solution, just a starting point!
Andrew
 
  • Like
Reactions: Frigg
Hello Andrew,
Thank you very much for your help, and explanations. It works perfectly.
I'm not a developper, and my knowledge is low. In spite of my tries, I haven't been able to find by myself what was going wrong.
Hervé
 
Hello Andrew,
Thank you very much for your help, and explanations. It works perfectly.
I'm not a developper, and my knowledge is low. In spite of my tries, I haven't been able to find by myself what was going wrong.
Hervé
Could you please post your script?

I'm new on Proxmox and your solution could be perfect for me.
 
I think it should check for 1 and not 0. So something like:
Code:
#!/bin/bash
qm list | grep running
if [ $? -eq 1 ]; then
    shutdown -h now
fi

But to really use such a script you would need to check way more stuff if you don't want to run into problems. For example the script shouldn't run right after booting when autostart didn't started VMs yet. And the script will only look for VMs and not LXCs.
 
I think it should check for 1 and not 0. So something like:
Code:
#!/bin/bash
qm list | grep running
if [ $? -eq 1 ]; then
    shutdown -h now
fi

But to really use such a script you would need to check way more stuff if you don't want to run into problems. For example the script shouldn't run right after booting when autostart didn't started VMs yet. And the script will only look for VMs and not LXCs.
A timer could help? On startup or shotdown to give you time to call some other VM before shotdown.
 
A timer could help? On startup or shotdown to give you time to call some other VM before shotdown.
Jup. And some other problematic conditions:
- node shouldn't ne shutdown while a scrub is running
- not when a resilvering is running
- in case of a cluster it gets very complex because you don't want to loose quorum
- some kind of maintaince mode would be required if you don't want a shutdown while maintaining the server without running VMs

So thats alot of work if oyu want a script that is ready for production.
 

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!