Script to shutdown node when no VM active for 1 hour

Frigg

Active Member
Sep 4, 2016
57
2
28
49
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.
 

udo

Famous Member
Apr 22, 2009
5,918
180
83
Ahrensburg; Germany
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
 

guletz

Famous Member
Apr 19, 2017
1,523
238
83
Brasov, Romania
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).
 

Andrew Hart

Member
Dec 1, 2017
68
11
8
50
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

Frigg

Active Member
Sep 4, 2016
57
2
28
49
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
 

Andrew Hart

Member
Dec 1, 2017
68
11
8
50
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

Frigg

Active Member
Sep 4, 2016
57
2
28
49
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é
 

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 your own in 60 seconds.

Buy now!