Multiple Operations in Hookscript Failing

tonystriker

New Member
Oct 21, 2026
4
0
1
https://forum.proxmox.com/threads/using-the-pct-qm-command-within-a-hook-script.125614/ for reference.

I am using the hookscript to revert a vm to a prior snapshot with the nohup command suggested above:
Code:
nohup /usr/sbin/qm destroy "$1" &>/dev/null &

This works fine enough, but I'd like the snapshot to be powered off, and then the hookscript power it back on after reverting. When I try some combination of
Code:
nohup /usr/sbin/qm rollback "$1" "snapshot" &>/dev/null &
nohup /usr/sbin/qm start "$1"
it errors out immediately because I have two operations trying to lock the lockfile. Obviously I'd need to wait for the first to complete, but I'm not quite sure how to do that. I tried some variation of "wait $!" in between but that didn't work. Whats the simplest way to do a revert and startup in the same post-stop hookscript?
 
& runs the command line in parallel with the lines after it, but you want to run 'start' after the 'rollback'. This is not specific to Proxmox and other shell scripting guides will apply.
Maybe you want ( nohup /usr/sbin/qm rollback "$1" "snapshot" &>/dev/null && nohup /usr/sbin/qm start "$1" ) & instead? This runs 'start' when/after 'rollback' ends successful, in parallel with the rest of the script. Or maybe just remove the & to not run those two things in parallel?

EDIT: Why would you run start on the VM itself ($1) as Proxmox will do this automatically? Can't you run the rollback without the & in the pre-start, so that it is rolled back before Proxmox starts it? Or is it locked by Proxmox during the hookscript/pre-start? If it is locked then you cannot fix it with running stuff in parallel. Maybe forcefully unlocking it before rollback might work?
 
Last edited:
  • Like
Reactions: UdoB
& runs the command line in parallel with the lines after it, but you want to run 'start' after the 'rollback'. This is not specific to Proxmox and other shell scripting guides will apply.
Maybe you want ( nohup /usr/sbin/qm rollback "$1" "snapshot" &>/dev/null && nohup /usr/sbin/qm start "$1" ) & instead? This runs 'start' when/after 'rollback' ends successful, in parallel with the rest of the script. Or maybe just remove the & to not run those two things in parallel?

EDIT: Why would you run start on the VM itself ($1) as Proxmox will do this automatically? Can't you run the rollback without the & in the pre-start, so that it is rolled back before Proxmox starts it? Or is it locked by Proxmox during the hookscript/pre-start? If it is locked then you cannot fix it with running stuff in parallel. Maybe forcefully unlocking it before rollback might work?
Thanks. I'm not sure why I hadn't tried running it like that. I will later test it as one line. If I take a snapshot of a powered off VM, it rolls back to powered off and does not start automatically. I do not need or want to capture the RAM, so I could also uncheck the box, but a snapshot powered off seemed "cleaner" to me. The VM in question is set to power itself off periodically to trigger the hookscript, so there is no separate power-on trigger.
 
I'm not sure but your reply sounds to me that you run all this in a post-stop phase. Then nohub and & can work (and you don't have to worry about the VM being locked) but you need to run both commands in serial (and the whole in parallel): ( nohup /usr/sbin/qm rollback "$1" "snapshot" &>/dev/null && nohup /usr/sbin/qm start "$1" ) &.

I would do a rollback in a pre-start phase when I want to run a VM from a known state (for testing or something) but that might run into the VM lock (as I mentioned before). I don't know which approach is best.