How to run shell scripts on the Android OS shell from within your Java/React Native Android app?

Utkarsh Opalkar
4 min readAug 4, 2021

Steps to get any shell commands/script running on a rooted Android device through an Android app —

Prerequisites:

  • Having a rooted device helps, but I don’t think it is necessary.
  • Android Debug Bridge installed on your computer.
  • USB cable to connect the Android device to your computer (I use a MacBook Pro)

Steps:

  1. Connect Android phone to computer with USB cable.
  2. Open new Terminal window on your computer.
  3. Run “adb shell” command on the Terminal.
  4. Run “ls” next.

Here’s what step 3 and 4 outputs should look like —

5. Run “cd data/local/tmp” next.

6. If you’re doing this for the first time, this directory is probably empty and doing an “ls” may not show anything. Also, “ls” at /data/ or at /data/local/ will probably give you permission denied, but go all the way in till data/local/tmp and you should be fine.

7. Now open any text editor and create a new file with the first line as -

#!/bin/sh

followed by whatever linux shell script you want to run on your android device. Save it somewhere convenient with the name my_shell_script_name.sh (you can give it whatever name you like but make sure to have the .sh extension at the end).  I save this file on the /Desktop directory for this tutorial if you wanna follow along. We will need this path in the next step.

I saved my shell script file as “get_process_info.sh”. If you want to name your’s something else that’s no problem but then change the filename in the step number 10. “ps -e” is the command that lists out all running processes on a Linux based device, so here’s what my script file looks like -

8. Now leave the current terminal window as it is and open another fresh new Terminal window.

9. Run “cd Desktop” on this new terminal. (or whichever path you saved your script file in).

10. Next run “adb push get_process_info.sh /data/local/tmp” (replace ‘get_process_info.sh’ with your file’s name from step 7)

This step should push your script file into the location /data/local/tmp.

11. Go back to the previous Terminal window that we left open in step 8.

12. Run “ls” command. You should see your file listed in its output-

13. In this terminal itself, run “chmod 777 get_process_info.sh” next. (obviously replace filename with your’s) This step basically changes the read/write permissions of your shell script file to root user i.e. allows my script to fetch metadata of all system level processes as well. You may skip this step if root access is not needed for your script’s application in particular.

14. You may run “ls -l” to verify that the read/write permission did in fact get updated to root. It would look something like the image after step 15. (see highlighted in blue)

15. Next run the shell script file to verify if it yields the desired result. “./get_process_info.sh”. Here’s what the result would be -

16. Now, the difficult part is taken care of! You can invoke this script file’s output from within your Java app as follows —

String command = “su -s sh -c /data/local/tmp/get_process_info.sh” ;

you just need to place this code snippet in your Java app wherever you want it to run, say, when a button is clicked..

for the full code, just google “how to run shell commands from java android app” or refer to this stackoverflow post.

I would have shared the whole code file, but in my case the Java class/function are actually embedded in a React Native app which uses Native Modules to run a Java function/code on the device and then passes on the result, back to the React Native app. (Read the hyperlinked documentation for more information).

Hope this helps someone struggling with this, Happy coding!

--

--

Utkarsh Opalkar

This CS life did not choose me, I chose this CS life. Hated it, ran from it, regretted it, but had to accept my choices, now falling in LOVE with CS for real!