tags:

views:

34

answers:

2

I have a shell script that starts tomcat using 'catalina.sh start'. This script runs fine and starts tomcat which stays up even if I exit from my ssh session.

I am trying to invoke this script from Jsch

shell = new JSch();
session = shell.getSession(user, host, SSH_DEFAULT_PORT);
session.setConfig(config);
session.setPassword(password);
session.connect();
commandChannel = (ChannelExec) session.openChannel("exec");
commandChannel.setCommand(command);
commandChannel.setPty(true);
commandChannel.connect();

This calls my script fine and tomcat starts up, but as soon as the process exits, tomcat is given a shutdown signal and it shuts down. I tried to use setDaemonThread without success. Any idea why this is happening?

UPDATE: The script also does a couple of sudo operations unrelated to starting tomcat so it needs tty. The user is setup in sudoers so as not to require a password, so no prompt is needed.

+1  A: 

I believe that you shouldn't allocate a pty to the command for the command to run in the background and not be attached to a terminal. What happens if you do commandChannel.setPty(false) instead?

EDIT: are you actually running sudo and supplying a password in the script? Shouldn't you run it as root instead? If using sudo is what you want, after running the commands detach them from the terminal using disown or nohup (explanation)

Vinko Vrsalovic
I get 'sudo: sorry, you must have a tty to run sudo' , that's why I added it in the first place.
shipmaster
nohup worked, thanks!
shipmaster
+1  A: 

Does this problem happen only when your session exits immediately? It sounds like the command you run spawns a separate process. Perhaps you are not giving enough time for the other process to be spawned.

What happens if you execute your command with nohup?

Synesso
D'oh! Sniped again!
Synesso