2

I hava a .sh script which glues many other scripts, called by jsch ChannelExec from a windows application.

Channel channel = session.openChannel("exec");

((ChannelExec) channel).setCommand("/foo/bar/foobar.sh");

channel.connect();

if I run the command like "nohup /foo/bar/foobar.sh >> /path/to/foo.log &", all the long term jobs(database operations, file processing etc.) seems to get lost.

checking the log file, only find those echo stuffs(before and after a long term operation, calculate running time etc.).

I checked the permissions, $PATH, add source /etc/profile to my .sh yet none of these works.

but when I run the command normally (sync run, print all echo outputs to my java client on windows),all the things goes well.

it's a really specific prob. Hope someone with experience can help me out.

thank in advance.

Han

Han Zheng
  • 378
  • 1
  • 3
  • 13
  • Are you sure that they get lost, or could it be that they simply are still running when you check the output? – Paŭlo Ebermann Jun 03 '11 at 11:28
  • Also, try the same with a stock `ssh` client to see if it is related to JSch (I suppose not). – Paŭlo Ebermann Jun 03 '11 at 11:30
  • hi. I can make sure that they are lost. Some records ought to be loaded into the database if they worked. – Han Zheng Jun 04 '11 at 09:17
  • I tried to use ChannelShell to execute those commands, and everything goes well. After some test, I found I must wait on the client side for some commands to print outputs, even if I use nohup mode. I'll keep testing... – Han Zheng Jun 04 '11 at 09:35

1 Answers1

6

Solved!

A different issue that often arises in this situation is that ssh is refusing to log off ("hangs"), since it refuses to lose any data from/to the background job(s).[6][7] This problem can also be overcome by redirecting all three I/O streams.

from http://en.wikipedia.org/wiki/Nohup

My prob is, psql and pg_bulkload print their outputs to err stream.

In my script, I didn't redirect err streams.

Everything went fine by also redirecting err streams to the same log file.

nohup foo.sh > log.log 2>&1 &

Thanks to Atsuhiko Yamanaka, he created a great JSch library, and Paŭlo Ebermann for the documentation.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Han Zheng
  • 378
  • 1
  • 3
  • 13