How to execute system commands (linux/bsd) using Java
Your way isn't far off from what I'd probably do:
Runtime r = Runtime.getRuntime();
Process p = r.exec("uname -a");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null) {
System.out.println(line);
}
b.close();
Handle whichever exceptions you care to, of course.
Getting output from executing a terminal command in a java code running inside Cubieboard Platform
the code is right, just in the second line, I changed
"/bin/sh" to "/bin/bash"
And everything works!
sh == bash?
For a long time, /bin/sh used to point to /bin/bash on most GNU/Linux systems. As a result, it had almost become safe to ignore the difference between the two. But that started to change recently.
Some popular examples of systems where /bin/sh does not point to /bin/bash (and on some of which /bin/bash may not even exist) are:
Modern Debian and Ubuntu systems, which symlink sh to dash by default;
Busybox, which is usually run during the Linux system boot time as part of initramfs. It uses the ash shell implementation.
BSDs. OpenBSD uses pdksh, a descendant of the Korn shell. FreeBSD's sh is a descendant of the original UNIX Bourne shell.
For more information on this please refer to :
Difference between sh and bash
Execute grep command on Linux and capture result
output
has not been assigned when you do if check. Change your code like below:
Process p;
String output = null;
try {
String command = "grep searchString filename.txt";
System.out.println("Running command: " + command);
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((output = br.readLine()) != null) {
System.out.println(output);
// Process your output here
}
System.out.println("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
Java Command is not executed
I see two issues:
- Multiple arguments need to be split in Java already.
- Authentication with
sudo
.
Multiple arguments need to be split.
If you run exec("a b")
, the system will look for a command named a b
as one single String command name.
If you run exec("a", "b"), the system will look for a command named
aand pass
b` as argument to that program.
So what you want to do is execute("sudo", "/bin/sh", "-c", "echo 7 > /sys/class/gpio/export")
.
sudo
might require authentication
When you execute commands with sudo
, an authentication will be performed. If you execute multiple sudo
commands from the same process, the system will cache the authentication for convenience, but basically the authentication is required.
The authentication with sudo
usually means that you need to supply a password.
The reason why you sudo
this is that /sys/class/gpio/export
has permissions -w-------
(200) owned by root root
, which means nobody can read it and only root can write it.
You have a few options:
- Change the permissions of that file so that everybody can write it (not recommended):
chmod a+w /sys/class/gpio/export
. - Change the permissions of that file so that the user in question can write it:
setfacl -m user:cher:w /sys/class/gpio/export
- note that this only works if yoursysfs
is mounted withacl
option, and usually it isn't. I don't know if it's even possible to mountsysfs
withacl
option, I haven't tried myself. - Pipe the password to the sudo command:
exec("echo password | sudo /bin/sh -c \"echo 7 > /sys/class/gpio/export\"")
WARNING THIS IS DANGEROUS!!! - Use a graphical
sudo
replacement likekdesudo
- Change your
sudoers
configuration so that the user in question never needs to enter password forsudo
- not recommended.
How to execute a code n times from the terminal?
You can repeat terminal commands as mentioned in the following link.
http://www.mactricksandtips.com/2012/02/loop-repeat-terminal-commands.html
But I suggest you add a for loop and get the number of repetitions as an argument. So you can run the command the number of times you need.
Related Topics
What Is the Java's Internal Represention for String? Modified Utf-8? Utf-16
Keyword for the Outer Class from an Anonymous Inner Class
Abstract Class VS Interface in Java
Why Stringbuilder When There Is String
What Are Classes, References, and Objects
How to Query Xml Using Namespaces in Java with Xpath
When Is the @JSONproperty Property Used and What Is It Used For
Convenient Way to Parse Incoming Multipart/Form-Data Parameters in a Servlet
Inject an Ejb into Jax-Rs (Restful Service)
Why am I Getting Inputmismatchexception
Rotating Coordinate Plane for Data and Text in Java
Differencebetween Method Overloading and Overriding
Which Java Collection Should I Use