Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

ssh - jsch ChannelExec run a .sh script with nohup "lose" some commands

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...