Search code examples
pythoncroncentosurllib

URLLib.Request returning connection timout when executed with cron (CentOS)


I have a Python script that I want to execute daily with cron. The only script in wihch I have issues is in the one that it uses URLLib request, there I get a connection timeout error:

Traceback (most recent call last):

  File "/usr/lib64/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/lib64/python3.6/http/client.py", line 1254, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib64/python3.6/http/client.py", line 1300, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib64/python3.6/http/client.py", line 1249, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib64/python3.6/http/client.py", line 1036, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.6/http/client.py", line 974, in send
    self.connect()
  File "/usr/lib64/python3.6/http/client.py", line 1407, in connect
    super().connect()
  File "/usr/lib64/python3.6/http/client.py", line 946, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib64/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/lib64/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out

This is the cron execution:

0 3 * * * /usr/bin/python3.6 "/home/ngabioud/Scripts Python VM/ClickTableau.py"

Do you have any idea why this isn't working? The script works fine if I run it manually on command line.

Thanks!


Solution

  • It seems like an issue coming from the proxy.

    Using the answer from here as an example,

    It turns out that I had to set the proxy settings so I access AWS as myself rather than root. I ran the cron job as a Linux shell script rather then a Python script, and exported my http_proxy, https_proxy, and no_proxy settings found in ~/.bash_profile in the first lines of the shell script

    `export http_proxy=<http_proxy from ~/.bash_profile>
     export https_proxy=<https_proxy from ~/.bash_profile>
     export no_proxy=<no_proxy from ~./bash_profile>
     python <python script>`
    

    If you still can't sort it, then do state where you are hosting the cron-job as well as the output of the step below.

    Consider logging cron-job stdout and stderr. This helps much more than python's error trace.

    18 20 * * * python2.6 script.py > /test.log 2>/test.err &
    

    also check /var/log/messages for any information.

    Finally, why not try celery instead of cron? see here