If you can and do have full console logs, check for what happened when you sent the command quit. That's honestly the most important factor here, you can see if there was any character or input send before which would cause it to fail, or if you have a parameter set to simply restart the server on the quit command sent.
As for a loop, there's a ton of different ways, but without your full codes I can only suggest looking at examples and adding it however it fits your current setup. Any of the following should get you what you want.
Example 1
Example 2
Example 3
Example 3