简体   繁体   中英

jython killing parent process that spawns subprocess breaks subprocess stdout to file?

Let me start with what I'm really trying to do. We want a platform independent startup script for invoking a JVM with some system properties and a dynamically generated classpath. We picked Jython in particular because we only need to depend on the standalone jython.jar in our startup script. We decided we could write a jython script that uses subprocess.Popen to launch our application's jvm and then terminates.

One more thing. Our application uses a lot of legacy debug code that prints to standard out. So the startup script typically has been redirecting stdout/stderr to a log file. I attempted to reproduce that with our jython script like this:

subprocess.Popen(args,stdout=logFile,stderr=logFile)

After this line the launcher script and hosting jvm for jython terminates. The problem is nothing shows up in the logFile. If I instead do this:

subprocess.Popen(args,stdout=logFile,stderr=logFile).wait()

then we get logs. So the parent process needs to run parallel to the application process launched via subprocess? I want to avoid having two running jvms.

Can you invoke subprocess in such a way that the stdout file will be written even if the parent process terminates? Is there a better way to launch the application jvm from jython? Is Jython a bad solution anyway?

We want a platform independent startup script for invoking a JVM with some system properties and a dynamically generated classpath.

You could use a platform independent script to generate a platform specific startup script either at installation time or before each invocation. In the latter case, additionally, you need a simple static platform specific script that invokes your platform independent startup-script-generating script and then the generated script itself. In both cases you start your application by calling a static platform specific script.

Can you invoke subprocess in such a way that the stdout file will be written even if the parent process terminates?

You could open file/redirect in a child process eg, using shell:

Popen(' '.join(args+['>', 'logFile', '2>&1']), # shell specific cmdline
      shell=True) # on Windows see _cmdline2list to understand what is going on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM