I'm writing a jython code which use Jsch to run some shell instructions on a remote machine. I need to wait a specific shell instruction has finished before continuing the exectuion of the jython code on my local machine (=need to synchronize shell code on a remote machine).
To do that, when the specific shell instruction has finished i create a text file in the remote machine. Then in my jython code I open an sftp channel and try periodically to get the file (using the sftp.exception and a "while true" loop). The code wait until the file is downloaded.
My problem is that even when the file is created, the jsch function "get" doesn't manage to download the file and keep sending "sftp.exception : no such file or directory" .That makes me locked up in an infinite loop.
Here is my code :
#We generate the info files for each class of the classlist
for classname in classlist:
print ("Generating information file (format : .out) for the class %s " % (classname))
#We open a shell channel and run the commands "windchill shell" and then "InfoReport.sh classname".
channel_generate = gl_session.openChannel('shell')
ops = channel_generate.getOutputStream()
ps = PrintStream(ops, True)
channel_generate.connect()
ps.println("windchill shell")
ps.println('rm ' + self.wt_home + '/tmp/' + classname + '.txt')
#The following instruction is the one i need to synchronize
ps.println(self.wt_home + '/bin/' + self.command + " " + classname)
#I create a file to detect the end of the previous instruction
ps.println('echo ok > ' + self.wt_home + '/tmp/' + classname)
ps.println('exit')
ps.close()
#Here we wait until the synchro file is created.
#We connect an sftp server and test every 4 seconds if we can retrieve the file.
channel_synchro = gl_session.openChannel('ftp')
channel_synchro.connect()
#This loop is broken when the get is successful
while True:
try:
channel_synchro.get(self.wt_home + '/tmp/' + classname,'/home/dev/temp')
break
except jsch.SftpException:
time.sleep(4)
#Disconnect and close de sftp channel
channel_synchro.disconnect()
channel_synchro.close()
#Disconnect and close the shell channel
channel_generate.disconnect()
channel_generate.close()
I have tried different solutions : other type of loops, create a new channel in each iteration of the "while true" loop, i have thought about user rights problem, i have checked if the paths are exact and they are, i have checked if the sftp function of my machine works and it works. Nothing works, i'm still not able to solve this problem