Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Shell脚本等待命令永不结束_Android_Linux_Macos_Shell_Sh - Fatal编程技术网

Android Shell脚本等待命令永不结束

Android Shell脚本等待命令永不结束,android,linux,macos,shell,sh,Android,Linux,Macos,Shell,Sh,我正在编写一个脚本文件来运行shell中的Android测试用例,我有一些Android ADB命令来运行脚本文件中的测试用例以执行,还有wait来等待第一组测试用例结束,第一组测试用例结束后几秒钟开始 脚本文件:代码段 #!/bin/bash # set up code #First set of test cases adb -s emulator-5554 shell am instrument -w com.android.demo.app.tests1/android.support

我正在编写一个脚本文件来运行shell中的Android测试用例,我有一些
Android ADB命令
来运行脚本文件中的测试用例以执行,还有
wait
来等待第一组测试用例结束,第一组测试用例结束后几秒钟开始

脚本文件:代码段

#!/bin/bash
# set up code

#First set of test cases
adb -s emulator-5554 shell am instrument -w com.android.demo.app.tests1/android.support.test.runner.AndroidJUnitRunner &
adb -s emulator-5556 shell am instrument -w com.android.demo.app.tests2/android.support.test.runner.AndroidJUnitRunner &
wait # Second set test cases does not started because this **wait** never ends

#Second set of test cases
adb -s emulator-5554 shell am instrument -w com.android.demo.app.tests3/android.support.test.runner.AndroidJUnitRunner &
adb -s emulator-5556 shell am instrument -w com.android.demo.app.tests4/android.support.test.runner.AndroidJUnitRunner &
wait

echo '--------- Test cases done --------------'
如果我运行脚本,它将运行执行的第一组(test1和test2)用例,并且启动第二组测试用例的等待永远不会结束 但是androidstudio(Android的IDE)表示,test1和test2的测试用例已经完成,这是第一套。 注意:ADB在Android Emulator上运行测试用例。
请帮助我解释为什么我没有等待结束。

我使用以下代码进行了测试:

#!/bin/bash
#set 1
ls -la &
ls -la &
wait # wait here

#set 2
pwd &
pwd &
wait # wait here
代码按预期完美运行,没有中断或错误

请尝试在不运行test1和test2的情况下运行test3和test4,看看这是否有帮助


编辑:另外,从另一个答案中,您应该能够捕获上一个命令的PID(processID),并将该PID用作等待参数。

wait
等待当前shell的所有子级。在开始分叉测试用例之前,您可能已经分叉了一个孩子,如上面的示例所示。例如:

#!/bin/bash

sleep 40 &  # something unrelated you forgot to mention

# test case set 1:
sleep 5 &   # first test case
sleep 7 &   # second test case
wait  # this now will wait for the two test cases _AND_ the sleep 40 from above.
有时“不相关的”分叉的东西不容易发现,所以不要匆忙

如果这是您的问题的原因,您可能可以通过使用不同的shell从另一个子对象派生来修复它,如下所示:

(sleep 40 &)  # use a different shell to spawn off the child

wait
不会同时退出
adb
do的两个副本(以及在前面省略的“设置代码”中启动的任何其他后台进程)。你有责任提出这个问题来证明
adb
已经退出,因此shell的行为在任何方面都是意外的。您还有责任证明没有“设置代码”创建其他作业表项,这些作业表项引用仍在运行的作业,因此也可能成为
wait
目标。