Java jsch sudo shell script not working as expected, Linux, Unix - java

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

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

How can i get process id of the same process from which i am running pgrep command?

I am trying to get ProcessId of the same process from which I am running the pgrep command. But the InputStream is returning null.
If we run command pgrep terminal from terminal in mac we cant get the processid of terminal. but if we run lsappinfo command in terminal and search for terminal it is there along with the processid.
I want to get processId of the same process from which I am running the command, Is there any other efficient way to get processId from process name like that of pgrep.

Create a script to run multiple java jar in different terminal windows

I wrote an application in java that needs five players and a server.
I need to write a script that executes the jar of the server and of every single player in different terminal windows. How can I do?
I tried a script and worked but the jar opened in the same terminal window than I tried with xterm or konsole with flag --noclose but does not work (warning command: konsole not found)
#! /bin/sh
xterm --hold -e java -jar /Users/Marco\ 1/Documents/ing-sw-2019-Lentini-Marazzi-Marini/out/artifacts/server_jar/adrenalina.jar
for X in $(seq 5)
do
konsole --noclose -e java -jar /Users/Marco\ 1/Documents/ing-sw-2019-Lentini-Marazzi-Marini/out/artifacts/client_jar/adrenalina.jar gui
done
exit;
To run a process in the background from bash, you'll need to add an & to the end of your command, e.g.
java -jar /path/to/jar/my.jar &
Otherwise bash will wait until the command execution terminates.

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?

Shell Script Won't Run From Java Program

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.

Categories

Resources