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();
Related
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
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);
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.)
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.
There are two things to note right off the bat....
The shell script runs fine manually
A simple shell script (echo hello) that I wrote runs fine through java
So I have a shell script that I'm attempting to run through a Java process.
File sqlF = new File("path to deploy script");
Process proc = rt.exec(sqlF + "/deploy.sh");
proc.waitFor();
System.out.println(proc.exitValue());
When I run this code I get an ambiguous return value of "1".
Here's the shell script (because I imagine the issue may stem from here):
#!/bin/bash
mysql -u XXXX -h XXXXX < XXXXX.sql
mysql -u XXXX -h XXXXX database < DEPLOY-HELPER.sql
Any ideas as to why this would not execute properly from Java?
If you want to run a shell script you must explicitly invoke the shell and pass it the name of the script as an argument, as in
bash /path/to/script/deploy.sh
Neither Runtime.exec() nor ProcessBuilder know how to execute shell scripts themselves, they only know how to execute binary executables.