I’m trying to write a python script that does the following from within a minutely cronjob:
- tries to execute a url
- after 10 seconds if there is no response yet, abandon the response and immediately issue a command via os.system to restart the webserver.
The problem is that when my server crashes, it doesn’t return a response at all. If I were to just have my script time the response, the script will go on for 10 minutes or more. I want it to issue the restart immediately once it detects a slow response. I know such a script could be written in probably less than 5 mines of code, but I have no idea how to go about it.
From Python 2.6 on you can use the following, provide the timeout in seconds in the call to urlopen:
This sets the timeout only for this request, not globally like with socket.settimeout. But for older versions of Python that is probably your only option.