user3848207
user3848207

Reputation: 4907

Python script stops running after running command using os.system()

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.

Upvotes: 0

Views: 2619

Answers (2)

user3848207
user3848207

Reputation: 4907

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

Upvotes: 0

rajendrau
rajendrau

Reputation: 28

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.

Upvotes: 1

Related Questions