I am writing a ruby script in which I want to execute the android logcat -v time command in a child process and after some time kill the process (when the the parent is done doing xyz).
For example I have:
childpid = Process.fork {
adb -s <device-serial> logcat -c
adb -s <device-serial> logcat -v time>/path-to-file/forkLog.log
}
sleep(30)
#parent do thing else here ...
Process.kill("SIGHUP", childpid) #kill the child process
According to what I've read the adb logcat code is executed in another child sup-process, so when I try to do a Process.kill the childpid stops but its sub-process does not.
how do I kill the logcat -v time>forklog.log process from the parent process??
Typically, when you're using fork to call a bash script, you'll want to use Kernel::exec() instead of Kernel::system() or Kernel::`
Process.fork{exec('adb -s <device-serial> logcat -c && adb -s <device-serial> logcat -v time>/path-to-file/forkLog.log'}
The difference is that exec will replace the Ruby process with the shell script, while the other two will create a subprocess.
Related
Inside Android (not on adb), we need logcat '*:W' -d -f /mycache/log.txt to obey the *:W and only put Warning and Error lines into log.txt.
But -f appears to not obey the :W, and we get all the excess lines in the log.
Variations with > don't work thru .exec(), and I don't want to collect the output as a stream when the low-level command should obey me.
take out the '':
Runtime.getRuntime().exec("logcat *:W -d -f " + logFile.getAbsoluteFile());
It turns out .exec() calls the raw process loader, not a command shell, so it wouldn't have expanded the *, so the '' aren't needed or expected.
(I posted because I thought I already tried that;)
I am logging the data coming from top and putting it into a circular set of files. I am not executing top for one set of data and then rerunning for the next set, but instead using a read time out to specify when to go from one log file to the next. This is primarily done this way to remove the startup CPU load cost every time top is executed. The shell script file's name is toplog.sh and looks similar to this:
#!/data/data/com.spartacusrex.spartacuside/files/system/bin/bash
date
echo " Logging started."
fileCmp()
{
test `ls -lc "$1" | sed -n 's/\([^ ]* *\)\{4\}\([0-9]*\).*$/\2/;p'` $2 $3
}
oldest()
{
ls -rc $1 2> /dev/null |head -1
}
file=`oldest /mnt/sdcard/toplog.\*.gz`
echo " Oldest file is $file"
if [ -z "$file" ]; then
x=0
else
file=${file%%.gz}
file=${file##*.}
x=$file
fi
echo " x=$x"
top -d 20 -b | \
while true; do
file=/mnt/sdcard/toplog.$x.gz
while read -t 5 line; do
echo "$line"
done | gzip -c > $file
if fileCmp "$file" -le 300; then
date
echo " Failure to write to file '$file'."
exit
fi
x=$((($x+1)%10))
sleep 14
done
I execute this using nohup so that when the shell dies, this process still runs, like so:
$ nohup ./toplog.sh
But there's a problem. top terminates when I exit the shell session that executed that command, and I'm not exactly sure why. Any ideas?
To clarify, I'm logging on a Android phone. The tools are limited in functionality (i.e. lack some of these switches) and is why I am using top as it contains the output I want.
Version of busybox I'm using is:
BusyBox 1.19.2 (2011-12-12 12:59:36 GMT)
Installed when I installed Terminal IDE.
BTW, this phone is not rooted. I'm trying to track down a failure when my phone responds as if the CPU has spiked and won't go down.
Edit:
Well, I found a workaround. But the reason is a bit hazy. I think it has to do with process management and smells of a bug in the busybox ver that I'm using that was missed during regression testing.
The workaround is to wrap top with a useless loop structure like this: while true; do top; done. Through testing, top never gets killed and never gets respawned, but by wrapping it up, it isn't killed.
Any insights on this?
going to sound stupid, but change your startup command from
nohup ./toplog.sh
to
nohup ./toplog.sh &
the & makes it run as a background process further removing it from the terminal stack.
Running the bash internal command "disown" on your script's process before logging off may prevent it from being signaled.
I was trying to run Mobile originated call perl script on Emulator but getting error while running the script:
Below is the script:
Mo_call.pl
#!/usr/bin/perl -w
use strict;
use New_MO.pm;
for(my $i=0; $i<=4;$i++)
{
New_Mo::call_Originate();
}
New_MO.pm
package New_MO;
sub call_Originate
{
system("adb -s $device_id shell service call phone 763726728");
sleep 10;
system("adb -s $device_id shell input keyevent 4");
system("adb -s $device_id shell input keyevent 3");
}
1;
I am new for this things so if possible then please let me know where I am doing mistake.
Thanks
You should try using
use New_MO;
instead of
use New_MO.pm;
If this doesn't help, would you please share the error message you're getting?
I want to capture logs and dump them into a text file, I am using a perl script to do this. The issue I am running into is that I want to include only certain tags in my logcat command.
I am using
$adbcommand_logcat = "start \"Android-Logcat\" cmd /c \"adb -s $sno logcat -s ^(?=.*?\babc\b)(?=.*?\bxyz\b)(?=.*?\bpqr\b).*$ | -v threadtime | tee ".$mainlog_filename."\"";
but this gives me a blank log instead of the log with tags specified in the regex. If I take out -s after logcat then the regex does not works and log includes everything.
Any help is appreciated.
I got it to work using the following command with egrep.If you don't have the tag try to find the process name and use it instead.
$adbcommand_logcat = "adb -s $sno logcat -v threadtime | egrep \"tag1 or Process 1| tag2 or Process 2\" |tee ".$mainlog_filename;
I've been told it's a command line option. But Eclipse's Run!Run Configurations...!Target!Additional Emulator Command Line Options field is already occupied with
-sdcard "C:\android-sdk-windows\tools\sd9m.img"
If I wanted to write something like
adb logcat -s MessageBox > "C:\Users\me\Documents\LogCatOutput.txt"
then where do I write it, and how (i.e., is the syntax even correct)? I need to output only a filtered tag, not verbose. ("MessageBox" is my TAG. Again I don't know if any of this punctuation is right, or even where the command goes.)
Thanks for any help.
There should be an adb.exe file in C:\android-sdk-windows\tools. You can invoke this manually from a DOS command prompt:
cd C:\android-sdk-windows\tools
adb logcat -s MessageBox > "C:\Users\me\Documents\LogCatOutput.txt"
There's no need to bother with Eclipse in this case.
Alternatively, if you only want to dump whatever's already in the logcat buffers and exit immediately (useful for scripts), you can specify the -d option:
$ adb logcat -d -s MessageBox > dump_file.txt
Make sure that '-d' is after 'logcat'.
Another useful addition to the above answers is filtering. The application I am working on generates a massive amount of logging, so it is useful to log with filters.
adb -s MyDevice logcat | find /V ": T#" > d:\temp\logcat.txt
or for OSX/Linux
adb -s MyDevice logcat | grep -v ": T#" > ~/logcat.txt
This will write all logcat activity apart from any line that contains ": T#"
find or grep can also be used to filter based on positive results. I then use the likes of BareTail display the growing log file.