How to Execute System Commands (Linux/Bsd) Using Java

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:

  1. Modern Debian and Ubuntu systems, which symlink sh to dash by default;

  2. Busybox, which is usually run during the Linux system boot time as part of initramfs. It uses the ash shell implementation.

  3. 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 namedaand passb` 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 your sysfs is mounted with acl option, and usually it isn't. I don't know if it's even possible to mount sysfs with acl 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 like kdesudo
  • Change your sudoers configuration so that the user in question never needs to enter password for sudo - 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



Leave a reply



Submit