
Tengo un script que verifica si hay máquinas virtuales ejecutándose y, si las hay, las apaga elegantemente (usa VBoxManage), la idea es que si me llaman fuera de mi máquina, las bases de datos que se ejecutan en las máquinas virtuales no sufrirán. problemas por saltos de tiempo cuando se reanudan.
¡El script se ejecuta correctamente desde la línea de comando, pero no cuando se llama desde el archivo suspend-to-hibernate.service que he creado!
Lo que debería suceder es que el script llame a VBoxManage para enumerar las máquinas en ejecución, lo canalice a awk para extraer el nombre de la VM y lo descargue todo en otro archivo que luego se llama para cerrar las máquinas.
Funciona bien hasta la llamada a VBoxManage; parece que el comando simplemente no se ejecuta. /usr/bin/VBoxManage es en realidad un enlace a un script que llama a /usr/lib/virtualbox/VBoxManage. Intenté llamar a esto directamente pero todavía no se ejecuta.
stop-vms.log muestra que el script "sobre la marcha" realmente se ejecuta.
Ha pasado un tiempo desde que hice algún script en Unix y siento que me he perdido algo muy simple...
suspender-para-hibernar.servicio:
[Unit]
Description=Script to check for VirtualBox VMs and pause them
Conflicts=hibernate.target hybrid-suspend.target
Before=suspend.target sleep.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/sh /home/steve/Documents/scripts/stop-vms.sh suspend
[Install]
WantedBy=sleep.target
RequiredBy=suspend.target
detener-vms.sh:
#!/bin/sh
# some vars:
NEWSCRIPT=/home/steve/Documents/scripts/stopvms
SHELL=/bin/sh
LOGFILE=/home/steve/logs/stop-vms.log
# check if script file exists, if not create & make executable
if [ ! -e $NEWSCRIPT ]
then
touch $NEWSCRIPT
chmod 775 $NEWSCRIPT
fi
# tell the script where the shell is
echo '#!'$SHELL > $NEWSCRIPT
echo '#starting at:' `date` >> $NEWSCRIPT
# get list of running machines and dump into script file
echo 'echo "running vms" >> /home/steve/logs/stop-vms.log' >> $NEWSCRIPT
/usr/bin/VBoxManage list runningvms | awk '{print "/usr/lib/virtualbox/VBoxManage controlvm " substr($line, 1, index($line,"{")-2) " acpipowerbutton >> /home/steve/logs/stop-vms.log "} ' >> $NEWSCRIPT
echo 'vms done, about to run script at ' `date` >> $LOGFILE
# run the script
$NEWSCRIPT
# now wait to make sure the previous command has had time to finish...
sleep 30
# report on last run...
echo >> $NEWSCRIPT
echo '# Last Run: ' `date` >> $NEWSCRIPT
Script stopvms esperado (cuando se ejecuta desde la terminal)
#!/bin/sh
#starting at: Wed 31 Jan 10:24:34 GMT 2018
echo "running vms" >> /home/steve/logs/stop-vms.log
/usr/lib/virtualbox/VBoxManage controlvm "Dev Mysql GlassFish" acpipowerbutton >> /home/steve/logs/stop-vms.log
# Last Run: Wed 31 Jan 10:25:04 GMT 2018
Real cuando se ejecuta desde suspender-to-hibernate.service:
#!/bin/sh
#starting at: Wed 31 Jan 10:59:26 GMT 2018
echo "running vms" >> /home/steve/logs/stop-vms.log
# Last Run: Wed 31 Jan 10:59:56 GMT 2018