我需要从bash脚本启动一些Genymotion模拟器;这是我的剧本:
#!/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
肯定我没有使用bash最佳实践,但是一旦我获得了工作版本,我就会提高代码的质量。 该脚本可以工作,但是,可能发生其中一个模拟器卡住,Genymotion播放器进程无法启动模拟器并且模拟器显示此弹出窗口:这是弹出图像的链接,因为我无法上传没有10个信誉点的图像 http://www.absolute-keitarou.net/blog/wp-content/uploads/2014/01/genymotion-error-1genymotion-error-1.png
现在,我想用bash检查进程是否以某种方式失败,如果失败则再次启动它。
我无法弄清楚检查进程是否失败的有用方法,我试图使用$?但它没有用,即使弹出窗口显示$?含有0。