How to solve permission when running bash command from java - java

When I run commands from the console everything is OK:
sudo -u oracle fgrep ...
When I run the same command from Java code using ProcessBuilder, sudo doesn't work, and I need to set chmod to 775 or else I don't have permission to read logs.
Why doesn't this work? Is there an option to read logs without chmod 775?
Here is how I am using ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
Process shell = pb.start();
InputStream is = shell.getInputStream();

Since you say chmod 775 for log file it works, it's obvious your process doesn't have permission.
You can run your java with sudo:
sudo java ClassFileName
Or just add sudo as the first string in the array that you pass to bash process:
command[0]="sudo -u oracle ";
//command[1]=commandname;
//command[2...n]=Other params;
Assuming user oracle is in sudoers list and won't ask for password, this will run just like how it runs in commandline when you use sudo.

a. You don't need the bash -c, when you're executing the command you have a shell.
b. The command needs to be split on spaces and then passed into the ProcessBuilder as an array.

Related

How to run a linux command using processbuilder WITHOUT elevated (sudo / root) permissions from an elevated java process?

I am running an elevated java process sudo java -jar ... and i want to be able to execute something using ProcessBuilder but without elevated permissions.
The exact command i want to run is sox -t pulseaudio default -t wav -. Running this with sudo prepended, it gives an error, this command has to be ran without sudo.
Running the command like so from the elevated java process
new ProcessBuilder().inheritIO().command("bash", "-c", command).start();
Results in having the command ran with sudo privleges, so the sox command gives an error.
How can i run the sox command without sudo privleges from an elevated java process?
EDIT:
i have attempted sudo -u USERNAME bash -c but that yields the exact same result, where sox cant open the default input

Java.Error when calling python script with Runtime

I have to calling python script in java project.I dont use jython because in script i using todoist-api. I tried use this:
try {
String cmd = "/home/kiryushin/projects/python/stm/venv/lib/stmtest1.py";
Process p = Runtime.getRuntime().exec(cmd);
}
catch (IOException e){e.printStackTrace();}
I get
" java.io.IOException: Cannot run program "/home/kiryushin/projects/python/stm/venv/lib/stmtest1.py": error=13, Permisson denied"
I try change permission with chmod -r 777, and other commands but i get this error again.
Ubuntu 18.04 lts. JDK 10. Intelij idea community edition.
Try with :
First manually check with sudo permission.
And modify:
String cmd = "sudo python /home/kiryushin/projects/python/stm/venv/lib/stmtest1.py"
use your command like this, echo your password then use sudo -S along with your command
String cmd = 'echo your_password|sudo -S python /home/kiryushin/projects/python/stm/venv/lib/stmtest1.py';
That's how I solve my problem
Recreate my project without virtual environments (venv)
Add shebang in python script
With pip3 add lib todoist-app
In calling python script i write:
String cmd = "python3.6 /home/kiryushin/projects/python/stm2/stm.py"; Process p = Runtime.getRuntime().exec(cmd);

Linux terminal command in Java does not work

I'm trying to execute a linux command in my java code. It needs to change permissions for some directory.
Here is my attempt:
String Cmd = "echo myPassword | sudo -S chmod 777 -R /home/somePath";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(Cmd);
The command held in String Cmd is working perfectly when I used it in terminal. But when I use it in my code nothing happens. There is no error or warning feedback that helps me to understand my mistake. What might be the problem?
Java will not magically select bash as your executable. You probably want to do something like
"bash -c <your command>"
See this question:
How to run unix / shell commands with wildcards using Java?
(Also the | is a bash-thing. Java won't magically create pipes between processes.)

Execute sudo command in ubuntu from java class

I have a sh file which conatins a many sudo commands, say a script as below:
test.sh
#!/bin/sh
sudo apt-get install gedit
sudo date --set="2014/02/20 10:00"
When I execute the test.sh from terminal it works fine. Problem is when I try to execute this file from a java class. The script doesn't execute. I tried this:
Process proc = Runtime.getRuntime().exec("/bin/sh","/home/priyatam/test.sh");
Does anyone have an idea to execute this sudo command from java class? Please share.
well as the script will be outputting the prompt to the screen, it will be necessary to capture the processes inputstream
InputStream stdout = process.getInputStream ();
I would also recommend using
ProcessBuilder builder = new ProcessBuilder("/home/user/test.sh");
builder.redirectErrorStream(true);
Process process = builder.start();

Executing Linux Command in Java

I'm trying to execute the following Command in Java in order to kill the spawned process of bash script which is executed through java :
kill $(pgrep -P $(pgrep -P 5537))
I'm using apache Commons Exec Commandline to build the Command but it's no different to using ProcessBuilder here. So here is what I have so far:
CommandLine cmdLine = new CommandLine("bash");
cmdLine.addArgument("-c");
cmdLine.addArgument("kill $(pgrep -P $(pgrep -P "+pid+"))");
I get the error
bash: $'kill 7940\n7941\n7942\n7943': Command not found.
Normally I would now try to get the newlines out of the Command but it also doesn't work to kill just a single process because then I get the error :
bash: kill 7980: Command not found.
One the one hand I need to use bash to use the variables and on the other hand I can't use it because kill can't be executed with it...
firstly kill -9 pidnumber
Why would you need the bash variables? when java gives you strings to store variables?
Thirdly why not try System.Runtime.getRuntime().exec() ?
Also do you have permissions to kill the task? tried sudo kill -9 pid?

Categories

Resources