[SOLVED] Crontab does not work

Proxfan2025

New Member
Sep 26, 2021
20
0
1
55
Hi,

I'm trying to autostart a VM every 5 min in case my users accidentally shut down it.
I added a line "qm start 900" by editing crontab but it doesn't work.

When I type "systemctl status cron", it seems it's calling the "qm start" command, but the session closes everytime.
There are some info noticing that cronjob requires full path for a certain command.

Is there any idea solving this?

Mar 12 20:00:01 test CRON[8146]: pam_unix(cron:session): session opened for user root(uid=>
Mar 12 20:00:01 test CRON[8147]: (root) CMD (qm start 900)
Mar 12 20:00:01 test CRON[8146]: pam_unix(cron:session): session closed for user root
Mar 12 20:05:01 test CRON[8959]: pam_unix(cron:session): session opened for user root(uid=>
Mar 12 20:05:01 test CRON[8960]: (root) CMD (sudo qm start 900)
Mar 12 20:05:01 test CRON[8959]: pam_unix(cron:session): session closed for user root
 
Thanks avw, it worked perfectly!
Below is the new result for "systemctl status cron".

Mar 12 20:55:01 test CRON[16379]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
Mar 12 20:55:01 test CRON[16380]: (root) CMD (/usr/sbin/qm start 900)
Mar 12 20:55:02 test qm[16381]: <root@pam> starting task UPID:test:00003FFF:0008AC8F:622C8A16:qmstart:900:root@pam:
Mar 12 20:55:02 test qm[16383]: start VM 900: UPID:test:00003FFF:0008AC8F:622C8A16:qmstart:900:root@pam:
Mar 12 20:55:03 test qm[16381]: <root@pam> end task UPID:test:00003FFF:0008AC8F:622C8A16:qmstart:900:root@pam: OK
Mar 12 20:55:03 test CRON[16379]: pam_unix(cron:session): session closed for user root

I appreciate your quick solution. :)
 
You could also first check if that VM is really shutdown before doing a "qm start" so you don't spam the logs with messages that the VM can'T be started because it is already running.

Something like this:
Code:
#!/bin/bash

VMIDLIST=(100 101 102 103) # list of VMIDs that should always be running

for vmid in ${VMIDLIST[*]}
do
    if [ $(/usr/sbin/qm status $vmid) == "status: stopped" ]; then
        /usr/sbin/qm start $vmid
    fi
done

Also take into account that a backup job might need to shutdown a VM for a short time.
 
Last edited:
Thanks Dunuin, that's absolutely helpful!

It's best if I can make "VM permanent boot list" to wake them up only when they are shut down. Crontab log can also be kept clean.

And yes, I will take into account that some hours/days be excluded for cold backup.
 
Last edited: