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.