jsch ChannelExec run a .sh script with nohup “lose

2019-01-26 00:25发布

问题:

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

回答1:

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.