Starting adb logs on internal storage without going into the shell - android

I am trying to start internal logs within my android devices without having to go directly into the adb shell. I have to script this process so I can run it from a program.
I am aware that you can take the following steps to start internal logs on an android device:
Open a cmd prompt
enter 'adb shell'
enter 'logcat -v time -f /sdcard/LogFile.txt&'
The above will start a logcat process within the actual device. I can now unplug my phone from the computer and move around then come back and collect the logs once my test is complete. It's crucial that I am able to start this process and be able to unplug my device with the logs still running.
To my knowledge running 'adb shell' in front of any command would run as if it were in the shell. Therefore by this logic I tried running:
Method 1:
'adb shell logcat -v time -f /sdcard/LogFile.txt&'
This command did properly start the log on the device which is great. However, once I unplug from my computer the logcat process stops.
Method 2:
'adb shell "logcat -v time -f /sdcard/LogFile.txt&" '
This didn't seem to do anything at all on the phone and I don't know why.
Method 3
I have tried the scripting method as well where I run a Batch file that contains only:
'adb shell < Commands.txt'
Where commands has the single line:
'logcat -v time -f /sdcard/LogFile.txt&'
This doesn't appear to do anything. It appears to send the command once the window comes up but doesn't actually perform the action.
Any help on this topic would be greatly appreciated. Thanks.

you can use this code in application class. so when app will launch this code will start executing. and it will create new log file according to current time.
Thread t = new Thread(new Runnable() {
public void run() {
while (collectLog == true) {
try {
Thread.sleep(1000 * 60 * 6);
StringBuilder log = null;
Date now = new Date();
String fileName = formatter.format(now);
File file = new File(dir, fileName + "_logcat2.txt");
try {
Process process = Runtime.getRuntime().exec("logcat -d");// d will dump logs
//Process process = Runtime.getRuntime().exec("logcat -c"); c will clear logs
// process = Runtime.getRuntime().exec("logcat -f " + file);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
} catch (IOException e) {
}
try {
//to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut)
// Write the string to the file
osw.append(log.toString());
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();

Related

Android Wear - code to read LogCat output programmatically and continuously is not working

Hi everyone I recently wrote some code to read LogCat output continuously within a background Thread started by a background Service in my app. I do not think there are errors in my code but it's not working.
In my Thread, I have:
#Override
public void run() {
Process process = null;
String cmd = "logcat -v time | grep 'AudioFocus requestAudioFocus()'";
try {
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("logcat -c" + "\n");
os.writeBytes(cmd + "\n");
os.flush();
} catch(Exception e) {
}
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
try {
while ( (line = reader.readLine()) != null ) {
Log.e(StaticVal.TAG, line);
// code for write line to my own log file
}
} catch(Exception e) {
}
}
I also added permission to manifest:
<uses-permission android:name="android.permission.READ_LOGS" />
However, when I plug my device with PC and test it with Android Studio, Log.e(StaticVal.TAG, line); did not print out the results and nothing wrote into my own log file.
Any help is appreciated!
(Directly run logcat -v time | grep 'AudioFocus requestAudioFocus()' on my device through adb terminal get the results as expected)

getRuntime Android show process

I'm doing a simple app that show the currently process in android, like a shell.
My app execute ls, cd, makedir and other commands, but top or htop command doesnt. (htop doesn't recognize, and top, the app freeze). I need root to this? I've downloaded the terminal app in unrooted android and top command works.
my app has 2 class. a principal and a shell
principal class
public void onClick(View arg0) {
// TODO Auto-generated method stub
ShellExecuter exe = new ShellExecuter();
command = input.getText().toString();
String outp = exe.Executer(command);
out.setText(outp);
Log.d("Output", outp);
}
shell class
public String Executer(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
Why some commands the app works and top for example doesn't??
If you have your Device connected, go to the shell to see what commands you have available using the following command in your computer's command line:
adb shell
At first glance you will be able to tell that "top" is a job that "displays and update sorted information about processes" and it blocks the shell commands line, hence, in your application is locking the thread that executed that command.
Hope it Helps!
Regards!
thanks. adb shell with top command show the process
but
p = Runtime.getRuntime().exec("top");
doesn't maybe works??
I don't understante cause my app freeze
or a top -n 1 not fixed?

Programatically disable System Bar (Have Root access)

I am able to disable the System bar using the following adb command using a terminal client:
adb shell service call activity 42 s16 com.android.systemui
However, I want to achieve this programatically. I looked up how to execute adb commands programatically and found below code:
Process process = Runtime.getRuntime().exec("your command");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
I replaced "your command" with my adb command above but it does not disable the system bar. How can I achieve this?
You will need root access to do this from an app because ADB can access /system/bin/service but your app cannot (see GID of app and ADB).
Process su = null;
try {
su = Runtime.getRuntime().exec("su");
String cmd = "service call activity 42 s16 com.android.systemui\n";
su.getOutputStream().write(cmd.getBytes());
String exit = "exit\n";
su.getOutputStream().write(exit.getBytes());
su.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (su != null) {
su.destroy();
}
}
You should also check out third party libraries for handling su commands: https://android-arsenal.com/details/1/451

execute pipes command in android?

I want to display application logs.
On terminal I used this command: adb logcat -s brief *:V|grep "pid"
It's display My Application logs.
pid means application pid which is display in logcat table.
public static String logProc()
{
String value = "";
try
{
String cmd[] = {"logcat","-s","brief","*:V","|","grep",
android.os.Process.myPid()+""};
Process p = Runtime.getRuntime().exec(cmd,null, null);
BufferedReader reader = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null)
{
value += line + "\n";
line = reader.readLine();
}
p.waitFor();
}
catch (IOException e1)
{
e1.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
Most official Android builds do not come with grep (Edit: more recent releases now do)
You can install busybox if you want extra commands - without root you would have to put it in an alternate location.
You have the additional problem that you are trying to exec() a shell command to connect two programs via pipes, which isn't going to work unless you exec a shell interpreter and give it that command. Or you could set up the pipe yourself and exec both programs.
But since you are writing a program, it would probably be simpler to do your pattern matching in the java code.

Syntax for shell command in Android app

I am trying to run
String command = "su -c 'busybox ls /data'";
p = Runtime.getRuntime().exec(command);
in my app, but it seems like the syntax is somehow wrong. I have no problem running it from the terminal emulator app on the phone, though, so I just can't understand why it is not working when called from within my app.
Any help is deeply appreciated!
SOLUTION FOUND! Thanks to the link suggested by onit here. See the code below: for superuser shell commands to work properly, you first need to create a superuser shell and assign it to a process, then write and read on it's input and output streams respectively.
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
//from here all commands are executed with su permissions
stdin.writeBytes("ls /data\n"); // \n executes the command
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<BUFF_LEN){
//we have read everything
break;
}
}
//do something with the output
Use the function below:
public void shellCommandRunAsRoot(String Command)
{
try
{
Process RunProcess= Runtime.getRuntime().exec("su");
DataOutputStream os;
os = new DataOutputStream(RunProcess.getOutputStream());
os.writeBytes(cmds+"\n");
os.writeBytes("exit+\n");
os.flush();
}
catch (IOException e)
{
// Handle Exception
}
}
Usage:
shellCommandRunAsRoot("pkill firefox");

Categories

Resources