0

My python script freezes after running os.system(). The code looks like something below;

command_line = 'java -jar "backup.jar" test'
os.system(command_line)
# python script freezes at this point. Cannot move on to execute code below

For some reason, I suddenly encounter this problem today. No problem in the past. There was no error message. No hints on what's the problem. I did an update to latest Windows 10 package. Not sure if this is the cause.

If this problem can't be fixed, are there alternatives to os.system() to run a command such that python script doesn't freeze?

I am using python 3.8 on anaconda.

user3848207
  • 2,358
  • 11
  • 33
  • 59
  • 3
    Python doesn't "freeze" here any more than waiting for any input. It does, however, wait for target program to *terminate* before returning from `os.system`. In any case https://stackoverflow.com/q/89228/2864740 covers different methods which allows running other processes 'asynchronously'. YMMV, as they'll also need to wait for the java process to end to guarantee execution finished. – user2864740 Apr 15 '21 at 06:24
  • It's not freezing, it might be executed in the background. How long have you waited for the program to finish? – Sridhar Raju Apr 15 '21 at 06:28

2 Answers2

1

Change the command like below to see if there are any issues with command.

command_line = 'java -jar "backup.jar" test > /tmp/test.log'

You can verify /tmp/test.log to see if any issues with java command from the log. os.system waits for response. Same can be achieved with subprocess.call() methods with return status (0 - if successful). To run command in background you can use subprocess.Popen() method.

rajendrau
  • 38
  • 5
0

I will answer my own question. It is based on the answer from rajendrau which I have marked as the correct answer. It works for me.

What I did was to use subprocess.Popen(). Script no longer "freezes" anymore.

subprocess.Popen(['java', '-jar', "backup.jar", 'test'])
user3848207
  • 2,358
  • 11
  • 33
  • 59