SHARE
TWEET

Untitled

a guest Aug 14th, 2019 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. logFile=./vbox-machines-shutdown.log
  4. timestamp=$(date +"%Y-%m-%d %T")
  5.  
  6. arr=($(vboxmanage list runningvms | grep -Po '(?<=\").*(?=\")'))
  7.  
  8. echo $timestamp machine: vbox-machines-shutdown.sh script executed >> $logFile
  9.  
  10. # check if there are any active machines
  11. if [ ${#arr[@]} -eq 0 ]
  12. then
  13.     echo $timestamp there are no active machines >> $logFile
  14.     exit 0
  15. fi
  16.  
  17. for (( i=0; i<${#arr[@]}; i++ ));
  18. do
  19.     echo $timestamp machine: ${arr[i]} is running, attempting to stop >> $logFile
  20.     VBoxManage controlvm "${arr[i]}" acpipowerbutton
  21.     sleep 5s
  22.  
  23.     isVMachineRunning=$(vboxmanage showvminfo "${arr[i]}" | grep -c "running (since")
  24.  
  25.     case "$isVMachineRunning" in
  26.         0)  echo $timestamp machine: ${arr[i]} has successfully stopped >> $logFile
  27.             notify-send "Virtual Machine ${arr[i]} is stopped"
  28.             ;;
  29.  
  30.         1)  echo $timestamp machine: ${arr[i]} [WARNING] problem occurred while stopping machine, second attempt to stop machine >> $logFile
  31.             VBoxManage controlvm "${arr[i]}" acpipowerbutton
  32.             isVMachineRunning=$(vboxmanage showvminfo "${arr[i]}" | grep -c "running (since")
  33.             case "$isVMachineRunning" in
  34.  
  35.                 0)  echo $timestamp machine: ${arr[i]} has successfully stopped >> $logFile
  36.                     notify-send "Virtual Machine ${arr[i]} is stopped"
  37.                     ;;
  38.  
  39.                 1)  echo $timestamp machine: ${arr[i]} [ERROR] machine didn\'t stopped!!! >> $logFile
  40.                     notify-send --urgency=critical "Virtual Machine ${arr[i]} didn't stopped!!! View logs for details"
  41.                     ;;
  42.  
  43.                 *)  echo $timestamp machine: ${arr[i]} [ERROR] unknown machine state!!! >> $logFile
  44.                     ;;
  45.  
  46.             esac
  47.             ;;
  48.  
  49.         *)  echo $timestamp machine ${arr[i]} [ERROR] unknown machine state!!! >> $logFile
  50.             ;;
  51.     esac
  52. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top