I am using Java-7 and process builder to run shell script using Tomcat 7.
processBuilder = new ProcessBuilder("/bin/bash", "/opt/script.sh");
processBuilder.start();
As the above mention code invoked via JSP page hosted by Tomcat7 Server, so the command run but process is not able to continue as it is initiated by tomcat7 user (Default user of tomcat) How can i run this command as root, assuming i know root password?
Run the command using sudo:
Effectively, sudo allows a user to run a program as another user (most
often the root user).
ProcessBuilder b = new ProcessBuilder("/bin/sh", "-c", "echo <password> | sudo -S /opt/script.sh");
b.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'm trying to build a java method to execute an 'sh' command on terminal using Process class. It looks like this:
Process p;
String[] cmd = { "sh", "/dir/file.sh"};
p = Runtime.getRuntime().exec(cmd);
After setting up some permissions I can fire the command manually through console without sudo credentials and it will do the job, but java runs from tomcat at the server and the tomcat user doesn't appear to get through permits, even though the target sh file has been given -rwxr-xr-x flags.
I was wondering how could I set the same cmd String[] for it to execute like root user, something like:
echo "password" | sudo -S sh /dir/file.sh
I tried this but it didn't work:
String[] cmd = { "echo 'password' |", "sudo -S", "sh", "/dir/file.sh"};
I have a shell script containing start and status methods.
When I run start method manually from the shell the command will be like
sudo -u x /y.sh start
Output will be Process has started.
After that if I run status method from shell the command will be like
sudo -u x /y.sh status
Output will be Process is running
If I run the shell script using Jsch sudo class
When running start method
I am getting output as Process has started
After that I run the status method I am getting the output as
Process is not running instead of Process is running as we started the process using start.
How to make the status start from Jsch?
Please suggest me....
I am placing the shell script code here
I am running this command with apache commons exec
C:\\Windows\\System32\\cmd.exe /C start ".\\test" /D ".\\test1" .\\test1\\ldecod.exe -p InputFile=\"test.h264\" -p OutputFile="test.yuv"
the command runs normally through command prompt i.e. starts ldecod.exe and decodes test.h264, but when running the same command through apache commons exec it just opens the folder "test1"
What's going on here?
edit: using the following code
String cmd = "C:\\Windows\\System32\\cmd.exe /C start \".\\test\" /D \".\\test1\" .\\test1\\ldecod.exe -p InputFile=\"test.h264\" -p OutputFile=\"test.yuv\"";
CommandLine commandline = CommandLine.parse(cmd);
DefaultExecutor exec = new DefaultExecutor();
exec.execute(commandline);
You need to add the /b argument to the windows start command. This will run the your command without starting a new command prompt.
From the docs:
/b - Starts an application without opening a new Command Prompt window. CTRL+C handling is ignored unless the application enables CTRL+C processing. Use CTRL+BREAK to interrupt the application.
The way you are running it, start is spawning a new process. I suspsect that ldecod.exe is running but the start command (and your program) does not terminate till you close the command prompt.
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.