Multiple commands on remote machine using shell script - java

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
'"

Related

not able to execute next statement in a shell script after running a java jar app using nohup

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

Can run jar from commandline but not in shell script - why?

I want t start a jar which should provide a Rest-WebService.
When I run following command from terminal the jar and the webservice starts successfully:
java -jar SchnittprofilService-1.0-fat.jar BH121 8888
If I run the same command in a shell script start.sh, the jar starts but the not the webservice.
The permission for start.sh is set to 777.
Any suggestions?
Oops:
You have to put your arguments in quotation marks:
java -jar SchnittprofilService-1.0-fat.jar "BH121" "8888"

Is there a way to run more than one jarfile using -cp or -jar as just ONE command?

Is there a way to run basically: java -jar file1.jar, file2.jar (Using only that one command, not using it twice in a row?)
Or using java -cp file1.jar file2.jar (Again, using the command only ONCE to run TWO .jar files?)
(Using Cmd Prompt/Terminal/etc)
If so, how?
java -classpath <your path>\file1.jar -jar file2.jar
Idea is provide one jar as primary jar and everything else can go to class path.
You can use nohup for this:-
nohup java -jar file1.jar & && nohup java -jar file2.jar &
Explanation:-
* nohup is used to run the bash commands back ground(Ex:- nohup ls &).
* && is used to combine two bash commands into a single command but run both
the commands
* Here I am trying to combine and run two background processes.
You can see these processes running using the bash command "top" and checking appropriate process no.
Hope this helps!
If you want the processes not to run in background and if you want to see the output live use this bash Command!
java -jar file1.jar & java -jar file2.jar &
Hope this helps!!

Jar not running

So I created a script the the following commands
#! /usr/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/user/Desktop/Hello.jar
USER=user
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"
And I saved it in
etc/init.d/
And then ran the following command in terminal
sudo update-rc.d java_server_launch.sh defaults
I have a program located at
/home/user/Desktop/
And it is called Hello.jar and it works fine when I run it. When I restart my computer for some reason the program (Hello.jar) does not execute. What am I doing wrong?
I'm doing exactly what the answer here says.
You need to replace Hello.jar with $MY_SERVER in the last line of your bash script. That's because your current working directory isn't /home/user/Desktop
Edit: Try replacing the last line of code with this:
/bin/su $USER -c "$JAVA -jar $MY_SERVER &"
if you're running on Ubuntu you should check out upstart
see how simple it is to run jar https://stackoverflow.com/a/12102542/41576

using rc.local to start a jar file at startup is not working

I have a .jar file I want to run whenever the system reboots/starts, so I put the line
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
in my /etc/rc.local file. The program is validated as working, and if I run the above command at the command line the program works as expected.
Other versions I have tried without success:
nohup /usr/bin/java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
and:
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar 2> /dev/null \ .. &
I am running centos 6.4.
Check that your jar file is accesible roots, NFS mounted volumes may impose special restrictions for root.
Instead of discarding your error messages, you might want to route them to syslog, something like 2> /sbin/logger -t FOO 1> /sbin/logger -t BAR
Maybe the path isn't set yet at startup time and you need the full path to the java executable or, possibly, nohup.

Categories

Resources