简短版本:
为什么我的bash脚本(在后台运行,通过sshd启动)不在每〜30秒时运行它?(注意:sshd和脚本运行为 root < /强>!)
我有一个脚本,即我开始使用 nohup bash script.sh &
,以便它在后台运行。
脚本应该检查特定应用是否使用 netstat
以及它使用 am start
和 task default: [ :unit_test_and_install, :espresso_test ] task :unit_test_and_install do sh './gradlew --console=verbose test installDebug installDebugAndroidTest' end task :espresso_test do devices = `adb devices` serial_numbers = devices.split(" ")[1..-1].map{|q| q.split(" ")[0] } threads = serial_numbers.map do |sn| Thread.new do sh "adb -s #{sn} shell am instrument -w -r -e package com.mycorp.myapp -e disableAnalytics true " + "com.mycorp.myapp.test/androidx.test.runner.AndroidJUnitRunner | tee tests_#{sn}.txt" end end threads.each &:join serial_numbers.each do |sn| grop = `grep "^OK .*tests" tests_#{sn}.txt` if grop == '' sh "cat tests_#{sn}.txt" abort 'FAILURES on ' + sn end end end 0
。 (这结果是不相关的)。
我已经注意到,即使脚本应该每30秒检查一次(当然大致),有时需要几分钟才能检查。我真的无法弄清楚为什么,它似乎完全随意(请注意,大多数时候手机的屏幕都在关闭的时候,一切都在后台运行。),所以我制作了一个测试标记,这是问题应该专注于简单的问题:
task default: [ :unit_test_and_install, :espresso_test ] task :unit_test_and_install do sh './gradlew --console=verbose test installDebug installDebugAndroidTest' end task :espresso_test do devices = `adb devices` serial_numbers = devices.split(" ")[1..-1].map{|q| q.split(" ")[0] } threads = serial_numbers.map do |sn| Thread.new do sh "adb -s #{sn} shell am instrument -w -r -e package com.mycorp.myapp -e disableAnalytics true " + "com.mycorp.myapp.test/androidx.test.runner.AndroidJUnitRunner | tee tests_#{sn}.txt" end end threads.each &:join serial_numbers.each do |sn| grop = `grep "^OK .*tests" tests_#{sn}.txt` if grop == '' sh "cat tests_#{sn}.txt" abort 'FAILURES on ' + sn end end end 1
就是这样。使用SSHD通过 task default: [ :unit_test_and_install, :espresso_test ] task :unit_test_and_install do sh './gradlew --console=verbose test installDebug installDebugAndroidTest' end task :espresso_test do devices = `adb devices` serial_numbers = devices.split(" ")[1..-1].map{|q| q.split(" ")[0] } threads = serial_numbers.map do |sn| Thread.new do sh "adb -s #{sn} shell am instrument -w -r -e package com.mycorp.myapp -e disableAnalytics true " + "com.mycorp.myapp.test/androidx.test.runner.AndroidJUnitRunner | tee tests_#{sn}.txt" end end threads.each &:join serial_numbers.each do |sn| grop = `grep "^OK .*tests" tests_#{sn}.txt` if grop == '' sh "cat tests_#{sn}.txt" abort 'FAILURES on ' + sn end end end 2
启动。脚本只做了除了打印当前的日期时间,并且归功于nohup将它写入文件。我离开了这个运行了一段时间,然后写了另一个脚本来检查结果并在上面的30以上时打印秒数。结果:
task default: [ :unit_test_and_install, :espresso_test ] task :unit_test_and_install do sh './gradlew --console=verbose test installDebug installDebugAndroidTest' end task :espresso_test do devices = `adb devices` serial_numbers = devices.split(" ")[1..-1].map{|q| q.split(" ")[0] } threads = serial_numbers.map do |sn| Thread.new do sh "adb -s #{sn} shell am instrument -w -r -e package com.mycorp.myapp -e disableAnalytics true " + "com.mycorp.myapp.test/androidx.test.runner.AndroidJUnitRunner | tee tests_#{sn}.txt" end end threads.each &:join serial_numbers.each do |sn| grop = `grep "^OK .*tests" tests_#{sn}.txt` if grop == '' sh "cat tests_#{sn}.txt" abort 'FAILURES on ' + sn end end end 3
这是从一个脚本的输出只运行4:20小时,所以执行的很多时间必须延迟,睡眠时间超过30秒,或者任何发生的东西....
有人可以解释这个吗?这是一个解决方案吗?
我怀疑这可能会与内存或电源管理有关暂停脚本,但真的不应该发生,我希望我的脚本在后台运行并睡眠30秒,而不是暂停或延迟睡眠。在这个testrun中,最高的是467秒,但到目前为止,我见过的主要脚本的最高速度超过 20分钟。这很少见,但每天发生5-10分钟的延迟发生了几次。