The following script closes a virtual machine (which is run in a screen session), waits for the session to close, creates a backup of the VM, and restarts the VM. The shutdown and bootup scripts speak for themselves, but I can post them if necessary. Is there any way to clean up the sockets_found
function? It seems like there should be an easier way to detect whether or not screen has any open sessions.
#!/bin/bash
now=`date '+%Y%m%d'`
# No Sockets found
# There is a screen on
function sockets_found {
screen -ls | grep "There is a screen on"
if [ $? -eq 1 ]; then
return 1
else
return 0
fi
}
function wait_for_sockets_to_close {
while sockets_found; do
echo "Waiting for screen to close..."
sleep 1
done;
}
echo "Shutdown VM..."
/bin/bash ~/shutdown.sh
wait_for_sockets_to_close
# ensure that the backup directory exists
mkdir -p ~/backup
echo "Copying VM to backup directory..."
cp -Rf ~/VirtualBox\ VMs/ ~/backup/VirtualBox\ VMs${now}/
echo "Booting VM..."
/bin/bash ~/bootup.sh