0

I need to boot some Genymotion emulators from a bash script; here is my script:

#!/bin/bash -x
#########################
# Booting the emulators #
#########################

#Wait for ADB to get reliable
NUM_ELEM_ADB=$(adb devices -l | wc -l)
((NUM_ELEM_ADB-=2))

while [[ $NUM_ELEM_ADB -gt 0 ]]; do
  adb kill-server
  adb start-server
  NUM_ELEM_ADB=$(adb devices -l | wc -l)
  ((NUM_ELEM_ADB-=2))
done

#Retrieving all available emulators
EMULATORS=($(VBoxManage list vms | awk "{print \$NF}" | sed 's/[{, }]//g'))

#Boot EMULATORS[i]
for(( i= 0; i<${#EMULATORS[@]}; )) do
  #echo "###Booting emulator: "${EMULATORS[i]}
  /Applications/Genymotion.app/Contents/MacOS/player/ --vm-name ${EMULATORS[i]} &
  NUM_ELEM_ADB=$(adb devices -l | wc -l)
  ((NUM_ELEM_ADB-=2))
  ((i++))
  RESULT=$?
  echo "###################Result: $RESULT"

  #Wait for ADB to detect the started emulator
  while [[ $NUM_ELEM_ADB -lt $i ]]; do
    sleep 1
    RESULT=$?
    echo "###################Result: $RESULT"
    #Here I should check if the emulator crashed or not
    NUM_ELEM_ADB=$(adb devices -l | wc -l)
    let "NUM_ELEM_ADB-=2"
  done

  ((i++))
  DEVICE=$(adb devices -l | awk "NR==$i{print \$1}")
  COMMAND="adb -s $DEVICE shell getprop init.svc.bootanim"
  #echo $COMMAND
  OUT=`$COMMAND`

  # Waiting for the emulator to fully boot
  while [[ ${OUT:0:7}  != 'stopped' ]]; do
      #echo $COMMAND
      OUT=`$COMMAND`
      echo 'Waiting for emulator to fully boot...'
      sleep 1
  done
  adb -s $DEVICE shell input keyevent 82
  ((i--))
done
exit 0

for sure I am not using bash best practices, but I would improve the quality of the code once I get a working version. The script works, but, could happen that one of the emulator get stuck, the Genymotion player process does not start the emulator and the emulator shows this popup: here is the link of the popup image because I cannot upload image without 10 reputation points http://www.absolute-keitarou.net/blog/wp-content/uploads/2014/01/genymotion-error-1genymotion-error-1.png

Now, I would like to check with bash if the process failed in some way and if it failed start it again.

I can not figure out an useful way to check if the process failed, I tried to use $? but it did not work, even though the popup was shown the $? was containing 0.

shellter
  • 33,781
  • 7
  • 75
  • 89
visd0m
  • 46
  • 4
  • 1
    After `((i++)); RESULT=$?` the RESULT will show the result of i++. After `sleep 1 ; RESULT=$?` the result will be the result of sleep 1. When you start the emulator in the background, it might take some time before you can check for errors (not the next line of code). Can you sleep for 3 seconds and check the value of NUM_ELEM_ADB ? – Walter A Mar 01 '15 at 12:58
  • mmm yes, about the $? value I was doing smth stupid, I thought that $? was containing the exit code of the last background process. With the sleep yes it may work, but I would prefer to find out a way to understand exactly if the emulator crashed, and not wait for a certain time and assuming that smth went wrong because in that period of time the emulator should have started. Because when I start the player process in background I have also the PID with $!, is there a way, given a PID, to understand the state of the process? with ps I did not find out anything useful – visd0m Mar 01 '15 at 14:54
  • My best shot would be searching for a way that the program logs or writes on stdout/stderr, but I guess you already did. Perhaps http://stackoverflow.com/questions/18396344/how-can-i-script-genymotion-emulator-to-launch-a-given-avd-headless will help you, – Walter A Mar 01 '15 at 20:57

0 Answers0