Hooks and Event-Driven Automation
Overview
Hooks allow scripts to run on specific Proxmox VE events. React to VM lifecycle events automatically.
Hook Scripts
VM Hook Script
#!/bin/bash
# vm-hook.sh - Hook script for VM events
VMID=$1
EVENT=$2
log() {
echo "[$(date)] VM $VMID: $EVENT" >> /var/log/vm-hooks.log
}
case $EVENT in
pre-start)
log "VM starting..."
;;
post-start)
log "VM started"
# Notify, update status
;;
pre-stop)
log "VM stopping..."
;;
post-stop)
log "VM stopped"
# Cleanup, logging
;;
pre-migrate)
log "VM migrating..."
;;
post-migrate)
log "VM migrated"
;;
esacConfigure Hook
# Add hook to VM
qm set 100 --hookscript /root/vm-hook.shUse Cases
Startup Notification
#!/bin/bash
# notify-startup.sh
VMID=$1
# Send notification (Slack, email, etc)
curl -X POST https://hooks.example.com/notify \
-d "{\"text\":\"VM $VMID started\"}"
# Update monitoring
echo "$(date): VM $VMID started" >> /var/log/vm-updates.logBackup Trigger
#!/bin/bash
# backup-on-stop.sh
VMID=$1
EVENT=$2
if [ "$EVENT" = "pre-stop" ]; then
echo "Triggering backup for VM $VMID"
vzdump --mode suspend --vmid $VMID --storage local
fiResource Limits
#!/bin/bash
# adjust-resources.sh
VMID=$1
EVENT=$2
if [ "$EVENT" = "post-start" ]; then
# Check and adjust CPU limit
qm set $VMID --cpulimit 50
fiEvent Types
| Event | Description |
|---|---|
| pre-start | Before VM starts |
| post-start | After VM starts |
| pre-stop | Before VM stops |
| post-stop | After VM stops |
| pre-migrate | Before migration |
| post-migrate | After migration |
Configuration
Enable Hooks
# Add hook to VM
qm set 100 --hookscript /path/to/script.sh
# Remove hook
qm set 100 --hookscript noneTest Hook
# Manually trigger
/root/script.sh 100 post-startKeywords
hooks events automation lifecycle