2

I am working on my python script to launch a server, may be in background or in a different process and then further do some processing before killing the launched server.

Once the rest of the processing is over, then kill the launched server.

For Example

server_cmd = 'launch_server.exe -source '+ inputfile
print server_cmd
cmd_pid = subprocess.Popen(server_cmd).pid
...
...
... #Continue doing some processing
cmd_pid.terminate() # Once the processing is done, terminate the server

Some how the script does not continue after launching the server as the server may be running in infinite loop listening for a request. Is there a good way to send this process in background so that it doesn't expect for command line input.

I am using Python 2.7.8

1
  • can you include your full code. Commented Feb 11, 2015 at 9:36

2 Answers 2

2

It's odd that your script does not continue after launching the server command. In subprocess module, Popen starts another child process while the parent process (your script) should move on.

However in your code there's already a bug: cmd_pid is an int object and does not have terminate method. You should use subprocess.Popen object to call terminate method.

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

Comments

2

Making a small change resolved the problem

server_proc = subprocess.Popen(server_cmd, stdout=subprocess.PIPE)

server_proc.terminate()

Thanks Xu for correction in terminate.

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.