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.

2
  • 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 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. Commented Apr 15, 2021 at 6:24
  • It's not freezing, it might be executed in the background. How long have you waited for the program to finish? Commented Apr 15, 2021 at 6:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.