I'm developping an app which needs to run a command as root user so I use:
process = Runtime.getRuntime().exec("su");
Then I launch te process with:
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("tcpdump\n");
When I need the process to finish os.writeBytes("exit\n"); doesn't work and process.waitFor(); get's blocked and the process doesn't finish. I need to send Control-C to the process to stop it but I don't know how I could do it.
Thanks.
Find out whether the API has a kill() method somewhere and use that method to send the SIGINT signal to the target process.
On his github, Chainfire provides a sample implementation of a Shell class that you can use to execute commands as root. The class handles all the tasks using Threads so you can be sure that the command will not block even if it does not return.
Code Snippet:
if(Shell.SU.available()){
Shell.SU.run("commandAsRoot"); //Command executed as root
else{
System.out.println("su not found");
Or if you are certain that the su binary is available, you can just run your commands (commented line) and skip the check.
Source: How-To SU
Add this at the location you want a ctrl-c signal to be issued.
Process interrupt = Runtime.getRuntime().exec("su");
ios = new DataOutputStream(interrupt.getOutputStream());
ios.writeBytes("pkill -SIGINT tcpdump");
ios.flush();
ios.close();
interrupt.waitFor();
If there are multiple processes running by the name of tcpdump and you need to be selective, find out the specific process id using pidof and grep commands. Accept the answer if it worked for you. Let me know in comments if you are facing issues.
Related
I am running android-cts . The commands I run are mentioned below,
cts-tf
run-cts --plan --cts-camera
It is being running for two days . How do I stop the current task and save the existing logs .
Also it is mentioned in documentation ,that the cts logs will be stored in
CTS_ROOT/android-cts/results/start_time.zip
But I dont see a start_time.zip in the location specified .
There are few ways by which you can stop the CTS invocation and result will be generated of your runs,
Unplug the USB Cables from the devices, this will make tradefed to not detect any device and once timeout occurs, it will generate result on testcases and modules it ran.
Kill command, just write the kill in the tradefed, and it will kill and stop the invocation threads for the runs. once that is done, it will generate the result but make sure not to give kill command more than 1 or else it will exit from tradefed without generating the result.
I am developing with raspberry pi3 on Android Things.
I am trying to execute a Script on boot, modifying init.rc file as so:
sys.boot_completed = 1:
on property: sys.boot_completed = 1
bootchart stop
# WLD 201805031702
chmod +x /system/bin/myScript.sh //Added by me
sh /system/bin/myScript.sh //Added by me
I don't know why, but the Script doesn't get execute, i have tried to change "hostname anyName" instead of executing the Script on these lines of init.rc file, and it does pass throw these lines because hostname get changes, but it doesn't execute the Script.
How can i execute a Script on Boot, i don't know what to do.
Best regards
Alvaro
With Android Things, you can set up your application so that it fits within the Android framework.
For example, if you use the HOME intent filter on your main activity in your app, then your activity will launch immediately on boot, allowing you to run some code.
I'm using the RootTools library, and I need to execute two commands. The first one runs a binary, the second sends SIGINT to it, to kill it.
RootTools (as far as I know) can only have one root shell open at a time, so commands can only be executed one by one. This is a problem, because I have no way to stop my binary after I've ran it.
How can I do any of the following things?
Execute two commands at once, so I can run my kill command when the binary is running
Send SIGINT to my native process some other way (e.g. with a RootTools function)
I need to use RootTools because it's the only way for me to read standard output from my program. If there's another way to do that, though, please comment.
Do you think you can concat the commands?
Let's say I want to launch a find command, but if it takes 5 seconds, I want it to stop:
find / & sleep 5 && kill $!
We can get a better suited one liner, too (i.e. ignore standard error, kill only if needed etc.).
You could also just store the PID and kill it later (be careful, if the daemon stopped to run, his PID can be reused by the OS):
run the daemon in a root shell
my-daemon >/dev/null & echo "PID: $!"
parse the output in Java and store the PID (SharedPreferences?)
var pid = outputLine.split(" ")[1]
later on, stop the daemon with a root shell
kill <pid>
I have a requirement where I need to read an interface (say /sys/module/my_file/parameters/val) and then based on its value write some value on another interface. This has to be done in init.rc of Android filesystem.
flow would be like this
if ( read /sys/module/my_file/parameters/val == "yes") then
write /sys/devices/platform/target_file/val 100
Can anybody help me to do the same?? Is it possible??
You can implement this using an exec stanza:
exec <path> [ <argument> ]*
Fork and execute a program (<path>). This will block until
the program completes execution. It is best to avoid exec
as unlike the builtin commands, it runs the risk of getting
init "stuck".
See https://github.com/android/platform_system_core/blob/master/init/readme.txt for the details.
Example:
Move your code into a script (my_script.sh).
Add this to your init.rc
on boot:
...
exec /path/to/my_script.sh
You can use init’s service concept for running a script. Below is the code sample form init script.rc file.
chmod 0750 /system/bin/myscript.sh
start script
[...]
service script /system/bin/myscript.sh
class main
user root
group root
oneshot
My app needs to request SU access (on rooted devices) and the only examples out there say to do this:
Process p = null;
p = Runtime.getRuntime().exec("su");
That's fine, but how do I associate that p process with what I want to do next? For example how do I use that process to open a database so that the attempt to open the database happens with root permissions?
Please see this discussion:
Run secure API calls as root, android
Basically, you'd have to specify the stuff you want to execute in your call to the exec() method. Since that's hard to do, using the system app is really the best way to do things.
For the actual call, do this in your code:
Process p = null;
p = Runtime.getRuntime().exec("su sqlite3 your_query");