Run linux command on android

Can you run Linux command line apps on Android?

I’m brand new to Android; in fact I’ve never owned one. But I’m traveling all this summer, and I’m very interested in doing some programming while I’m on the run. QUESTION: Is it possible to run a full Ruby development environment with Emacs on Android? How? And does «rooting» give you anything close to a real Linux system?

Try this method. Basically any c, C++ compiled for ARM will work geeknizer.com/install-run-linux-applications-on-android

As a new user as well, Jackpal looks like it ticks the boxes as a reworking of the Android Terminal Emulator.

4 Answers 4

«Rooting» allows you access to the internal Android environment, which of course consists of more than just the Linux kernel. So you are running Linux, but you should consider it as though you are running under a different distribution than say Debian or Redhat.

There’s a different set of standard libraries and some files are in different locations. At least on my Droid ext2 support is not built-in by default. There is also no init or cron . The full set of Linux modules is not available unless you compile them yourself and put then in /system/lib/modules . Important ones you may miss on standard ROMs are cifs , ext2 , tun , and others.

Most, if not all, phones running Android are ARM CPUs. So the binaries you run on them must be ARM «EABI» «soft-float» binaries. If you have the source you can cross-compile whatever utilities you need, but read on.

There are many standard commands and utilities available in the /system/bin directory such as grep , ps , cp , rm , mv , ls , ip , even vi . You have enough to get by until you.

. use the «Linux Installer» utility to install a chroot ‘ed Debian environment. Debian has supported ARM for a while. It takes some work, but if you can root your phone, and have a sizeable enough SD card, this is the way to go.

This is close to a standard Linux system and under it you can install and run Ruby. Of course CPU and RAM limitations come into play.

Источник

How to Use Termux to Run Command Line Linux Apps in Android

This article covers a guide on the “Termux” Android app that allows you to run command-line programs and scripts on Android devices.

Termux is an open-source terminal emulator application that works on Android devices. It also works as a sort of mini Linux OS, packed with many tools and utilities you commonly see in desktop Linux distributions. You can use Termux to install and run numerous command-line apps through its own package manager. No root access is required to install and run Termux on Android. You can even use a lightweight desktop environment GUIs without hardware acceleration through Termux (via VNC), but they may be slow and not exactly usable on small screen touch devices. Termux is extremely popular among developers and other users who want to access CLI Linux apps on Android. It is the closest thing you get to a Linux OS on Android, and it is a pleasure to use with its touch-optimized interface suitable for small screen devices. Termux features additional keyboard actions making it easy to input symbols, and also features auto-completion through the action key located in the top row of the on-screen keyboard.

Читайте также:  Progressbar as in linux

Use Cases

Some things you can do with Termux:

  • Run Python scripts
  • Run Bash scripts
  • Play command-line games
  • Access Vi editor
  • Make SSH connections
  • Create Python virtualenv
  • Develop apps as long as you don’t need GUI access
  • Install additional packages with pip, npm, cpan, gem, tlmgr, and other such package managers
  • Basically, anything that an installed package allows you to do through its command-line interface

Installing Termux on Android

You can download and install Termux through Google Play or from F-Droid. Launch Termux through the launcher, and you should be greeted with the following screen:

Enabling Storage Access on Termux

To access files in the Termux terminal or to save files from the Termux terminal, you will first need to setup the Termux storage and provide storage access permissions to Termux when prompted. You can do so by executing the following command:

Once you are through the storage setup, you will be able to find Termux files stored in the “shared” folder in the internal storage of your Android device. If the “shared” folder doesn’t exist, you can manually create one. Usually, the full path to this “shared” folder is “/storage/emulated/0/shared”.

Installing and Managing Official Termux Packages

Once you have installed Termux, run the command below to update and upgrade repositories:

Now you can install your desired packages using the following command:

After installation, you will be able to run the command for the installed package in the Termux terminal (just like you would do on a desktop Linux OS):

You can get a list of installable Termux packages from here. You can also search and look for packages in Termux itself. To do so, run a command in the following format:

You can also list all packages using the following command:

Installing Deb Packages in Termux

You can install certain “.deb” packages from Ubuntu or Debian repositories as long as they are made for your mobile’s architecture (these days, mobiles mostly have aarch64 and aarch32 architectures). Note that some packages may refuse to work on Termux. To install a “.deb” package, run a command in the following format:

To remove a manually installed “.deb” package in Termux, run a command in the following format:

To list all manually installed “.deb” packages, you will need to run the following command:

Any “.deb” package from any package source can be installed as long as it meets compatibility requirements. As always, you should be careful when picking up third party packages to prevent the installation of suspicious packages.

Enabling Additional Repositories in Termux

You can also enable extra repositories in Termux to enable the installation of additional packages. To find more repositories, visit this page and click on repositories having names ending with “-packages”. You will find the command for enabling these repositories in their “README” files. The command for enabling extra repositories looks like this:

Читайте также:  Checkpoint mobile access vpn linux

Below are some examples that have I have tested and found working on Termux:

Some third-party community repositories can also be enabled. You will find a list of these repositories available here.

Installing Termux Add-ons

Termux provides some useful add-ons that can be installed on an Android device through the Play Store. Some of these extra add-ons are free, while others are paid. You can find a list of these add-ons available here.

Conclusion

Some apps on the Play Store allow you to install and run full Linux environments on Android. However, a few of them require root access, and they are not exactly easy to use. As far as user-friendliness is concerned, there is nothing else like Termux on the Play Store.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.

Источник

How to execute linux command in android programmatically?

As in android(through android sdk/tools folder) from command line we can execute linux shell command to access mnt folder/data folder likewise. (e.g cd data ls) now that command i want to execute from programmatically in android so how could it be possible? I am using following code to execute shell command

Process p = Runtime.getRuntime().exec("cd data"); 
java.io.IOException: Error running exec(). Command: [cd, data] Working Directory: null Environment: null 

5 Answers 5

cd is not a Linux command, it’s a command built into the shell; it changes the current working directory in the context of that shell process. In your case, if the command were to be successful, it would be successful for the child process only (which would soon terminate) and would have no effect on your own process.

What I’m saying is that executing a «cd» command is not going to have any effect on you. The reason for this is Runtime(). exec() will create a child process; even if you could get ‘cd’ to work in it, it would not affect your Android process so it would be useless to your Android app.

I’ve done a little Googling to see if there is a Java chdir() type function and I haven’t found much of use. One possibility (an ugly one, much more difficult than it is worth) is to create a native function which calls the C chdir() function. You would probably be much better off though just appending the full path to file names you refer to — then it would not matter what your current directory is.

ok this is my full path(data/data/androidCode) so how should i insert in my code to retrieve files from that folder?

On Android, your process does not have permissions to read files in other app’s /data/data/_other-package-name_ , or list its private files in directory /data/data/_other-package-name_/files . But it does have permission to list and read files in the lib directory /data/data/_other-package-name_/lib , and you can look at a specific file in /data/data/_other-package-name_/files , if the other-package opened this file as public.

I.e. if the other-package does something in line with:

FileOutputStream fos = openFileOutput("public_file", Context.MODE_WORLD_READABLE); fos.write("hello world".getBytes()); fos.close(); 

then your package can read this file like this:

byte[] bytes = new byte[100]; FileInputStream fis = new FileInputStream(new File("/data/data/*other-package*/files/public_file")); int cnt = fis.read(bytes); fis.close(); Log.d("Two_Libs", new String(bytes, 0, cnt)); 

But you cannot list the public files in that directory to discover them.

Читайте также:  Show all used ports linux

Источник

Any way to run shell commands on android programmatically?

Is there any way to run terminal commands on my application and then access the data on my UI? Specifically top .

5 Answers 5

Check out Log Collector as an example. Here is the relevant file.

ArrayList commandLine = new ArrayList(); commandLine.add("logcat");//$NON-NLS-1$ [. ] Process process = Runtime.getRuntime().exec(commandLine); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

so basically if I would like to run top, I could just replace commandLine with top and it should work fine right? I am getting an error so I guess I will need a little more help.. thanks..

Do I need to surround the Process process = Runtime.getRuntime().exec(commandLine); with a try and catch because I keep getting this throws IOException. I have looked at the java examples seem to handle it that way.

Yes.. again, check out the link to see everything they do. You definitely need to do error checking — don’t expect ‘top’ to be there on all phones.

Okay this is what exactly worked for me just in case anyone needs it in the future. 🙂

Surround in try and catch

try < Process process = Runtime.getRuntime().exec("top -n 1 -d 1"); BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); >catch (InterruptedException e)

When I do that followed by String result = null; result = bufferedReader.readLine(); Log.i(«OnClickListener», «result: » + result); It displays only «result:» (the ‘result’ string is empty). Any thoughts as to why this would be? Also, when I try process = Runtime.getRuntime().exec(«/system/bin/ping abc»); instead, the ‘result’ string is null. Both of those commands work as expected from an ‘adb shell’ terminal.

Sorry @DavidDoria, I have not touched android in 2 years now. It was something I tried my hand at while in college. You would be better off posting it as a question.

I got it working. The problem was the that the first line from ‘top’ WAS empty (a new line, to create the table). The case of it returning null when there was actually output was because I needed to get stderror (getErrorStream) instead of stdout (getInputStream).

We, can execute commands as follow, i was succesfull in doing this. try like this, here we need to specify the complete path of command. to get the complete path of commmand, at ur terminal (android) type

try < // Executes the command. Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard"); // Reads stdout. // NOTE: You can write to stdin of the command using // process.getOutputStream(). BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); int read; char[] buffer = new char[4096]; StringBuffer output = new StringBuffer(); while ((read = reader.read(buffer)) >0) < output.append(buffer, 0, read); >reader.close(); // Waits for the command to finish. process.waitFor(); return output.toString(); > catch (IOException e) < throw new RuntimeException(e); >catch (InterruptedException e)

it also depends on what you are running in the terminal. if you are running «cat» on a file you can also do it like this.

final private String MEM_FILE = "/proc/meminfo"; public Long readMem() < String[] segs; FileReader fstream; try < fstream = new FileReader(MEM_FILE); >catch (FileNotFoundException e) < Log.e("readMem", "Could not read " + MEM_FILE); return false; >BufferedReader in = new BufferedReader(fstream, 500); String line; try < while ((line = in.readLine()) != null) < if (line.indexOf("MemTotal:") >0) < Log.e("MemTotal", line); segs = line.trim().split("[ ]+"); memTotal = Long.parseLong(segs[1]); >if (line.indexOf("MemFree:") > 0) < Log.e("MemFree", line); segs = line.trim().split("[ ]+"); memFree = Long.parseLong(segs[1]); >> updateMem(); //call function to update textviews or whatever return true; > catch (IOException e) < Log.e("readMem", e.toString()); >return false; > 

Источник

Оцените статью
Adblock
detector