@@ -138,7 +138,7 @@ def make_content_disposition_header(fn):
138138import platform
139139from subprocess import Popen
140140from subprocess import STDOUT
141- from os import walk , kill
141+ from os import walk
142142import signal
143143import logging
144144
@@ -187,8 +187,8 @@ def make_content_disposition_header(fn):
187187EncodeUtf8 = 0
188188csvOutput = 0
189189
190- # Tika server PID, will be used later on to kill the process and free up ram
191- TikaServerPID = - 1
190+ # will be used later on to kill the process and free up ram
191+ TikaServerProcess = ""
192192
193193class TikaException (Exception ):
194194 pass
@@ -662,7 +662,8 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
662662
663663 # Run java with jar args
664664 # need to check if shell=true is really needed
665- cmd = Popen (cmd_string , stdout = logFile , stderr = STDOUT , shell = True )
665+ global TikaServerProcess
666+ TikaServerProcess = Popen (cmd_string , stdout = logFile , stderr = STDOUT , shell = True , preexec_fn = os .setsid )
666667
667668 # Check logs and retry as configured
668669 try_count = 0
@@ -681,12 +682,11 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
681682 log .error ("Tika startup log message not received after %d tries." % (TikaStartupMaxRetry ))
682683 return False
683684 else :
684- TikaServerPID = cmd .pid
685685 return True
686686
687687def killServer ():
688- if (TikaServerPID > 1 ):
689- kill ( TikaServerPID , signal .SIGTERM )
688+ if (TikaServerProcess . pid > 1 ):
689+ os . killpg ( os . getpgid ( TikaServerProcess . pid ) , signal .SIGTERM )
690690 else :
691691 log .error ("Invalid server PID, won't kill" )
692692
0 commit comments