jsch


How to add timeout when use JSch to execute a shell command which need user input later


There is one scenario in my case:
1.Execute a command : e.g. -bash-4.1$ testBackup
2.This command need to export some database name/password
3.But if I forget to export database name/password this command will be hang there.
4.I'm not quite sure how to use the JSch timeout logic when a shell command is hang
5.I use the JSch(exec) to execute shell command, it think it quite same with JSch(shell)
Here is the JSch code:
public String runCommand(String command) throws JSchException, IOException {
channel = sesConnection.openChannel("exec");
((ChannelExec) channel).setCommand(command);
BufferedReader br = new BufferedReader(
new InputStreamReader(channel.getInputStream()));
InputStream error = ((ChannelExec) channel).getErrStream();
channel.connect(60000);
outputstream = new StringBuffer();
String msg = null;
while ((msg = br.readLine()) != null) {
outputstream.append(msg).append("\n");
}
LOG.info("The output stream's length is:" + outputstream.length());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int code = channel.getExitStatus();
LOG.info("Channel exit status:" + channel.getExitStatus());
LOG.debug(
"Output of command successful execution: " + outputstream.toString());
String errormsg = null;
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(error));
errorstream = new StringBuffer();
while ((errormsg = errorReader.readLine()) != null) {
LOG.error("Terminal error output is: " + errormsg);
errorstream.append(errormsg).append("\n");
}
channel.disconnect();
if (errorstream.length() > 0) {
return errorstream + outputstream.toString() + "ExitCode" + code;
} else {
return outputstream.toString() + "ExitCode" + code;
}
}

Related Links

Creating a new file in SSH enabled system using JSCH
How to interact with the script after its execution using Jsch
java jsch -executing multiple unix commands
How to get one stream from error stream and input stream when calling a script using JSCH
SFTP using jschexception verify false sometimes fails
how to increase the jsch buffer size?
The cipher 'aes256-cbc' is required, but it is not available
How to pause/resume procedure while/after upload file by ChannelSftp
jsch + running multiple command in shell after SU and then close channal
NGit/Nsch Generating SSH Keypair (c# Jgit)
Extract userInfo from JSCH stream
Running a Shell Script using java ( process Builder ) with a specific unix user
Java jsch and resuming file upload after interruption?
How to pass a variable through channel in Jsch?
How to handle passwords prompts in expectj
Retaining the last modified date while using ChannelSftp for file transfer

Categories

HOME
kde
fft
tizen-web-app
malloc
graphql
jxls
baqend
primary-key
resize
gz
worldwind
transformation
propel
evopdf
pythonanywhere
dbext
autocad-plugin
zapier
scriptcs
graphicsmagick
chromebook
traffic
filezilla
linkerd
replaceall
libuv
file-rename
iframe-resizer
ioio
create-table
fog
grails-3.1
simplexml
scaffold
abstract-class
mesos-chronos
preconditions
checkboxlist
slick-3.0
avconv
flickr-api
atomicity
dropbox-php
aurelia-validation
statsd
zendesk-app
flutterwave
webdriverjs
visual-c++-2008
bonobo
qcustomplot
python-stackless
msys
simple-framework
purge
0xdbe
formatjs
typekit
pick
client-side-templating
uitouch
contenttype
htmlcleaner
jboss-weld
ember-charts
mcts
tidy
ril
cos
orchardcms-1.7
gdata-api
datawindow
javaspaces
windows-phone-7.1.1
heartbeat
xmemcached
rfc1123
javax.script
file-comparison
android-sdk-2.1
lang
infrastructure
castle-monorail
user-friendly
photoshop-cs4
ugc
commodore
misv

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App