ADB process blocks when starting background process - android

I am trying to run a shell script in the background on an Android phone via ADB. To simplify let's make it sleep 100:
$ adb shell
$ echo "nohup sleep 100&" > /data/local/tmp/test.sh
$ sh /data/local/tmp/test.sh
(does not block and returns to the shell immediately as expected. However:)
$ exit
(blocks until the sleep process is done)
Doing the same thing through a single adb command line is blocking as well:
$ adb shell sh /data/local/tmp/test.sh
Does run the script correctly, but the adb call blocks until 'sleep 100' is done. The sleep process keeps running if I CTRL-C out of adb, so the nohup part seems to be working correctly.
How can I get adb to exit after spawning the subprocess without forcefully killing the adb process on the host side?

adb shell 'nohup sleep 10 2>/dev/null 1>/dev/null &' works as expected - starts the process and does not block.

Related

Wait for Android emulator to be running before next shell command?

I started an Android emulator using the following shell command:
emulator -avd TEST_AVD
The emulator starts just fine, but the shell script never finishes executing. It just hangs there even after the emulator has completed startup. I have tried with a number of other arguments that I could find, but nothing could quite do what I want it to. How do I know, or stop the shell command, when the emulator is ready to go?
I am setting up our Jenkins CI to use a Jenkinsfile to start the emulator, and then run a series of gradle commands. In short, I'd like to do this:
sh "emulator -avd TEST_AVD"
sh "./gradlew clean test spoon"
However, I don't want to run the gradle tasks until the emulator has finished startup, and I can't figure out how to do that in the terminal.
If you want to do something after you start the emulator you should start it in the background
emulator -avd TEST_AVD &
adb wait-for-device
# other stuff here
adb can wait for a device over a transport to be in a particular state
adb wait-for[-<transport>]-<state>
- wait for device to be in the given state:
device, recovery, sideload, or bootloader
Transport is: usb, local or any [default=any]
To wait until device (or emulator) boots, you can do something like this (as was already answered by Пионерка):
adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'
Basically:
Wait for device/emulator to be in adb device state
Open shell & sleep in 1 second intervals until the sys.boot_completed property becomes true
If anybody would be interested what Android Studio does, when running emulator, the answer is this class:
If device is online, then it is ready. No need to go to further steps.
Checks system property adb shell getprop dev.bootcomplete until it is equal to 1
For API 23+ devices runs command to unlock screen: adb shell wm dismiss-keyguard
Waits 1 second.

How do I start one shell script as process from another shell script

I want to do this from my script1.sh which is run on host
#!/bin/sh
# adb shell "sh /sdcard/script2.sh &"
script2.sh is on android device inside /sdcard/ folder.
I want script2.sh to keep running on Android. script2.sh has infinite loop.
But above command is not working. script2.sh stops as soon as script1.sh exits.
I have also tried
# adb shell "exec sh /sdcard/script2.sh &"
but this also doesn't work.
In your parent shell script, extract the pid of the child process script2.sh.
and then wait for its completion using the wait system command.
wait <PID>
Refer this link for further details.
adb shell "nohup sh /sdcard/script2.sh &"
Instead of exec you can use nohup. What nohup does is it makes your script2 as child of init process (parent of all processes) as soon as script1 exits.

adb shell <command> does not print to stdout

Dead simple question. My program runs for 12s, and then prints to stdout. When starting it with adb shell command, it does not even start...
$ adb shell
# /data/myprogram
CPU time: 12682.4 ms
$ adb shell /data/myprogram
<nothing>
$
My program runs for 12s, the log is a std::cout C++ print in stdout. I use adb-v1.0.32. My dev board runs android-5.0.2, with root access. When trying to execute it with adb shell, it returns to the prompt quickly. I checked if the program ran in background with busybox ps, it does not.
What am i missing?

how to run binary in android without adb shell

when you run a binary in adb shell, if you exit adb, then the process is over too.
So is there a way to run a binary without adb's help?
If you have busybox installed on your device, then you could use 'busybox nohup [your binary] &' command. This way it will keep running even after you close adb
This works for me:
sh -c "your-binary-or-command" &
Then you can exit the adb shell by pressing ctrl+D and it will continue to run.
You can kill it by going back into the adb shell then
kill -s KILL <PID>

Android Emulator Shell Scripting with Ruby

We are developing a ruby script that executes a bunch of shell commands to launch the emulator and run some calabash tests.
PID = fork do
Signal.trap('HUP') { puts 'PROCESS ENDED'; exit }
exec 'emulator -avd TestDevice1'
end
fork do
sleep(55)
exec 'adb shell input keyevent 82'
end
fork do
sleep(60)
exec 'calabash-android run ~/MyApp/MyApp.apk'
Process.Kill('HUP', PID)
end
We are currently using sleep commands so that the calabash tests don't run until the emulator is fully ready. This is not ideal. Is there an Android command to check if the device is ready? By that I mean Android has booted up and the lock screen is displayed.
The most reliable way I have found to detect if the emulator is ready for use, and for Calabash to start the installation process, is to detect when the bootanim has stopped.
You can check whether the emulator has finished booting manually with using ADB in a terminal:
adb shell getprop init.svc.bootanim
I have the following in a Rake command as part of a Calabash test suite which does the trick:
booting = ''
while booting != 'stopped'
booting = `adb shell getprop init.svc.bootanim`.strip
puts 'Waiting for emulator to boot'
sleep 2
end
Hope it works for you!

Categories

Resources