0

We are running a java process, we want to stop execution after timeout, on timeout we get exception for timeout but java process is still running in background since we are using shell=True

Here is similar example

subprocess.run("ping 8.8.8.8",timeout=1, shell=True,)

however after timeout child process is still running

subprocess.TimeoutExpired: Command 'ping 8.8.8.8' timed out after 0.9998808979980822 seconds 64 bytes from 8.8.8.8: icmp_seq=2 ttl=122 time=10.7 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=122 time=10.1 ms

Some post suggested to drop shell=True, this does work for simple case, however for our java program we want to specify JAVA_OPTS but without shell we cannot set environment variables.

Is there a way to use subprocess.run which would kill child process post timeout expires

Vikrant Pawar
  • 789
  • 1
  • 6
  • 23
  • Does this answer your question? [Using module 'subprocess' with timeout](https://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout) – Dschoni Aug 13 '20 at 09:40
  • For environment variables: https://docs.python.org/3/library/subprocess.html `env` – Dschoni Aug 13 '20 at 09:42
  • Thanks @Dschoni these do solve some of issue but not these once exactly – Vikrant Pawar Aug 13 '20 at 11:53
  • 1
    You do have several issues that are not neccesarily connected. You could open a subprocess with a timeout as in my first comment. You could drop `shell=True` when piping environment variables to the subprocess in the first place. You could kill the subprocess manually after some timeout in the main thread.... etc. pp. – Dschoni Aug 13 '20 at 13:26

0 Answers0