Executing Linux Command in Java - 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?

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 jsch sudo shell script not working as expected, Linux, Unix

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

Java - apache commons exec, abnormal execution behaviour

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.

Java :Kill process runned by Runtime.getRuntime().exec()

I need to write a code,that
run unix process with Runtime.getRuntime().exec("java -jar MyServerRunner -port MYPORT");
find PID of the process by executing command from java code lsof -t -i: MYPORT
and kill him by pid kill -9 PID ( also by executing command from java code)
and then execute others commands
BUT
if I execute this command by Runtime.getRuntime().exec() my program exits with exit code 137 - this means that when I run Runtime.getRuntime().exec("kill -9 PID") I kill process of My java programm, but not the program, that I run from code.
How can I kill ONLY the process that I run from code ?
P.S. maybe I should use ProcessBuilder ?
You can kill a sub-process that you have launched from your java application with destroy:
Process p = Runtime.getRuntime().exec("java -jar MyServerRunner -port MYPORT");
p.destroy();
Also note that it might make sense to run that other code in a separate thread rather than in a separate process.
you can use .exec("ps|grep <your process name>");, and then parse the result to get the PID, finally .exec("kill PID");
Therefore, your process is killed but android app still alive.
You can get pid with reflection in unix (I know it is a bad idea :)) and call kill;
Process proc = Runtime.getRuntime().exec(
new String[] {"java","-classpath",System.getProperty("java.class.path"),... });
Class<?> cProcessImpl = proc.getClass();
Field fPid = cProcessImpl.getDeclaredField("pid");
if (!fPid.isAccessible()) {
fPid.setAccessible(true);
}
Runtime.getRuntime().exec("kill -9 " + fPid.getInt(proc));

How to solve permission when running bash command from 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.

Categories

Resources