@@ -138,7 +138,8 @@ def make_content_disposition_header(fn):
138138import platform
139139from subprocess import Popen
140140from subprocess import STDOUT
141- from os import walk
141+ from os import walk , kill
142+ import signal
142143import logging
143144
144145log_path = os .getenv ('TIKA_LOG_PATH' , tempfile .gettempdir ())
@@ -186,6 +187,9 @@ def make_content_disposition_header(fn):
186187EncodeUtf8 = 0
187188csvOutput = 0
188189
190+ # Tika server PID, will be used later on to kill the process and free up ram
191+ TikaServerPID = - 1
192+
189193class TikaException (Exception ):
190194 pass
191195
@@ -657,6 +661,7 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
657661 return False
658662
659663 # Run java with jar args
664+ # need to check if shell=true is really needed
660665 cmd = Popen (cmd_string , stdout = logFile , stderr = STDOUT , shell = True )
661666
662667 # Check logs and retry as configured
@@ -676,8 +681,15 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
676681 log .error ("Tika startup log message not received after %d tries." % (TikaStartupMaxRetry ))
677682 return False
678683 else :
684+ TikaServerPID = cmd .pid
679685 return True
680686
687+ def killServer ():
688+ if (TikaServerPID > 1 ):
689+ kill (TikaServerPID , signal .SIGTERM )
690+ else :
691+ log .error ("Invalid server PID, won't kill" )
692+
681693def toFilename (url ):
682694 '''
683695 gets url and returns filename
0 commit comments