I launch a jar from the console and it runs fine:
nohup /usr/bin/java -jar payara-micro-5.183.jar myapp.war > log.out 2>&1 & echo $!
When I run the same command from a distant computer with sshj, it returns a pid but the process can't be seen in the console, and actually the process doesn't run. In both cases I log via ssh, public key, with the same user.
The code running the command via sshj:
try (Session session = sshClient.startSession()) {
final Command cmd = session.exec("nohup /usr/bin/java -jar payara-micro-5.183.jar myapp.war > log.out 2>&1 & echo $!");
String response = (IOUtils.readFully(cmd.getInputStream()).toString());
// response shows a pid number
cmd.join(2, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
//exit status is 0
}
Solution:
With a previous command, I had cdto the directory where the jar is situated.
but this cp doesn't remove the need to use full paths for files used in the commands run after
Related
Not able to execute other instruction after if I am running a java jar file (which is present on another host) from my shell script.I tried using nohup but still not able to exit.
following is my script
#!/usr/bin/env bash
sshpass -p "${array[1]}" ssh -oStrictHostKeyChecking=no ${array[0]}#${array[2]} "cd ${array[3]} && echo -ne '\n' | nohup java -jar myapp.jar";
#some other instructions
echo "next statement"
already tried Scripts with nohup inside don't exit correctly but it doesn't worked.
sshpass -p pass ssh -o StrictHostKeyChecking=no xyz#hostname 'cd dir; nohup java -jar myapp.jar'
Try this with replacing the values appropriately
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 am working on a minecraft control panel in Ubuntu and thus I need to start/stop a .jar filewith shell_exec();
When I try commands like "whoami", the output is normal. But when I try this:
shell_exec("screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui");
It does not do anything, I have checked the permissions too and www-data is the owner of the files
Try to redirect the standard error stream to stdout (by appending 2>&1 to the command), fetch that output and print it to check whether there was a meaningful error message
$cmd = "screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui";
$redirect = '2>&1';
// using variable substitution only for readability here
shell_exec("$cmd $redirect", $output);
var_dump($output);
I am executing a bunch of SQL scripts and trying to read the output log file, looking for errors, after each file is executed.
Here's how I execute the script.
String strCommand = "sqlcmd -S SERVERNAME -d DBNAME -U USERNAME -P PASSWORD -r0 -i \"SCRIPT.sql\" 2> \"OUTPUT.LOG\" 1> NULL";
Process process = Runtime.getRuntime().exec(strCommand);
process.waitFor();
I've redirected the standard output to NULL and errors to the log file. Immediately following the execution of the above statements, I try to read the contents of the OUTPUT.LOG file, using File Reader and BufferedReader. An exception is thrown when trying to open the OUTPUT.LOG file.
(The system cannot find the file specified)
I check the path where the file should be saved, and needless to say, it isn't there. When I try to manually execute the command via Command Prompt, it works and writes the log file with errors, as expected. What am I doing wrong?
Your redirections (the 2> etc) will work if you are running from a command shell, but not if you run directly from java like this.
Try the following (for Windows):
String strCommand = "cmd /c sqlcmd -S SERVERNAME -d DBNAME -U USERNAME -P PASSWORD -r0 -i \"SCRIPT.sql\" 2> \"OUTPUT.LOG\" 1> NULL";
Process process = Runtime.getRuntime().exec(strCommand);
process.waitFor();
Note the addition of cmd /c, which will run the string in a command shell.
I have a Java program Desktop/testfolder/xyz.jar on a remote machine. It has a configuration file on the same folder. When I SSH into the machine, I do:
"ssh user#remote java -cp Desktop/testfolder/xyz.jar Main"
The problem here is the configuration file is not in the path, as we are in the home folder so my program cannot read the configuration.
I want to first go into that folder and then run the program from that folder. In a shell script if I did this
"ssh user#remote cd Desktop/testfolder"
"java -cp xyz.jar Main"
it executes the first statement and when the second statement is run it runs on my current machine not the remote machine.
Can we do only one command or there are any other solutions for this?
Try something like this:
ssh you#yours.com "cd /home && ls -l"
You could try separating the commands by a semicolon:
ssh user#remote "cd Desktop/testfolder ; java -cp xyz.jar Main"
If you want to split your commands over multiple lines for the sake of readability, you could also pass the list of commands to the bash command as follows:
ssh user#remote.host bash -c "'
cd Desktop/testfolder
java -cp xyz.jar Main
'"