[SOLVED] Bash script to start stop based on status - Crontab

turk00111

New Member
Mar 24, 2024
3
1
1
Hello,

I am trying to start/stop vm's based off status of other VM's. However, I am unable to get the script to work.

Here is my bash script:

Bash:
#!/bin/bash

if [ "$(qm status 107)" != "status: running" ]; then
    /usr/sbin/qm start 107
    /usr/sbin/qm shutdown 106
fi

Cron tab:

* * * * * /bin/bash /var/scripts/checkvm.sh

When I run this via the shell it works just fine and reports the correct status. When it is ran via crontab it does not report the qm status 107 correctly. When I echo and output to a file it is blank and does not have "status: running".

I am hoping someone on this forum may know what I am doing wrong and help pulling the correct status to be used in a script.
 
Last edited:
You are correctly using absolute paths inside IF, but for some reason not in IF condition.
Cron does not load user variables so it has no PATH.


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
Yes, I tried it with the absolute path before but it didn't work. However it wouldn't work.

I just got it working and typing out what I did. Your comment led me to the idea.
 
I just solved this thanks to bbgeek17. Thank you again!

here is my working bash file.


Bash:
#!/bin/bash

vm107=`/usr/sbin/qm status 107`

if [ "$vm107" != "status: running" ]; then
    /usr/sbin/qm start 107
    /usr/sbin/qm shutdown 106
fi
 
  • Like
Reactions: Sub7