cron job to start VM if not running

jaytee129

Member
Jun 16, 2022
142
10
23
I'd like to set up a cron job that runs every 5 mins does the following:

Code:
if "qm status 101" not equal to "running" then execute "qm start 101"

I need help with the if syntax that will do what I'm looking to do, and the crontab entry.

I think for the crontab entry I need to use:

Code:
*/5 * * * * checkvm.sh

is that right?

what's the code I need to put in my checkvm.sh script?
 
Something like qm status 101 | grep -q running || qm start 101 might work for you. It reads like: if detecting running in the output of qm status fails, then run qm start.
 
  • Like
Reactions: jaytee129
Thanks! Added the following line to /etc/crontab and it works great. every 5 mins it starts VM 101 if not running, otherwise does nothing.

Code:
*/5 *   * * *   root    qm status 101 | grep -q running || qm start 101
 
What's kind of nice and unexpected is that the cron job doesn't start VM while it's being backed up (and is stopped for/by the backup). It seems to consider it as 'running'... Don't understand why but it's a good thing.
 
What's kind of nice and unexpected is that the cron job doesn't start VM while it's being backed up (and is stopped for/by the backup). It seems to consider it as 'running'... Don't understand why but it's a good thing.
I'm not sure, it might be that qm start just fails. qm status 101 | grep -q stopped && qm start 101 only starts it if it is stopped.
 
I'm not sure, it might be that qm start just fails. qm status 101 | grep -q stopped && qm start 101 only starts it if it is stopped.
Wouldn't there be an error message in syslog? There's no message at all unless the VM ends up starting but that's message from the VM starting.

Where would cron error messages go? I don't see a cron log in /var/log
 
  • Like
Reactions: chamseddine
Beside putting every VM in a new line.... someone has a script to do this check for ALL VMs on a Node or a whole Cluster?

Thanks in advance......
 

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!