Ssh – Start and shutdown tomcat via ssh

sshtomcat

Updated

I find out that the path of jdk it using is wrong. eval: 1: /opt/Java/jdk1.6.0_25/jre/bin/java: not found the Java should be lower case java, how is that happen? When I run this script directly on server, it is just okay.


I'm trying to start or shutdown tomcat via a remote client.

On my server, I've got 3 different tomcat: tomcat1, tomcat2, and tomcat3.

Firstly, I've tried to run tomcat_path/bin/shutdown.sh to stop it via ssh, and the command is

ssh jake@1xx.3x.2x.1xx "cd /home/jake/tomcat2/bin;exec bash ./shutdown.sh"

both " and ' are tried, but do not work, the output is

eval: 1: /opt/Java/jdk1.6.0_25/jre/bin/java: not found

it seems that the shell script runs on my local client, because on server it has this file.

Is there any way to run a shell script on remote server correctly?

updated

I've run

ssh jake@1xx.xx.25.100 "sh -x /home/jake/tomcat/bin/shutdown.sh > /home/jake/tomcat.log 2>&1"

and the output in tomcat.log is :

+ PRG=/home/jake/tomcat/bin/shutdown.sh
+ [ -h /home/jake/tomcat/bin/shutdown.sh ]
+ dirname /home/jake/tomcat/bin/shutdown.sh
+ PRGDIR=/home/jake/tomcat/bin
+ EXECUTABLE=catalina.sh
+ [ ! -x /home/jake/tomcat/bin/catalina.sh ]
+ exec /home/jake/tomcat/bin/catalina.sh stop

eval: 1: /opt/Java/jdk1.6.0_25/jre/bin/java: not found

Best Answer

I find out that the path of jdk it using is wrong. eval: 1: /opt/Java/jdk1.6.0_25/jre/bin/java: not found the Java should be lower case java, how is that happen? When I run this script directly on server, it is just okay.

Because you've set the wrong environment variables somewhere (may be ~/.bashrc). bash reads the different startup files based on the different shell types. Keep in mind that:

  • ssh user@host 'command' is a non-interactive shell
  • whereas ssh user@host and run the command is an interactive shell.

http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files