auto514
Posts: 5
Joined: Thu Mar 28, 2019 5:24 pm

Why am I getting a name resolution error with my cron job?

Mon Apr 08, 2019 6:47 am

Hello,

This is a follow up to this thread.

I am trying to get a cron job to run and unfortunately for some reason nothing happens. I decided to have the script run on boot and surprisingly, this time around, my var/mail/pi file told me that something is wrong with my script.

Here is the contents:

Traceback (most recent call last):

Code: Select all

  File "/home/pi/Desktop/directory/file.py", line 106, in <module>
    function(param)
  File "/home/pi/Desktop/directory/file", line 13, in function
    server = IMAPClient(host, use_uid=True)
  File "/usr/local/lib/python3.5/dist-packages/imapclient/imapclient.py", line 254, in __init__
    self._imap = self._create_IMAP4()
  File "/usr/local/lib/python3.5/dist-packages/imapclient/imapclient.py", line 289, in _create_IMAP4
    self._timeout)
  File "/usr/local/lib/python3.5/dist-packages/imapclient/tls.py", line 44, in __init__
    imaplib.IMAP4.__init__(self, host, port)
  File "/usr/lib/python3.5/imaplib.py", line 189, in __init__
    self.open(host, port)
  File "/usr/local/lib/python3.5/dist-packages/imapclient/tls.py", line 49, in open
    sock = socket.create_connection((host, port), self._timeout.connect)
  File "/usr/lib/python3.5/socket.py", line 694, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution
This script runs without error manually, but for some reason whenever I try to run it with cron it does nothing. I changed paths and tested this script on a PC in Task Scheduler and did not have any problems.

pcmanbob
Posts: 9033
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Why am I getting a name resolution error with my cron job?

Mon Apr 08, 2019 7:49 am

" Temporary failure in name resolution " would suggest that when you are running the program you do not have access to the internet.

Do you have internet access can you browse the web ?

If you run the script manually after your pi has booted does it work then ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

auto514
Posts: 5
Joined: Thu Mar 28, 2019 5:24 pm

Re: Why am I getting a name resolution error with my cron job?

Mon Apr 08, 2019 3:32 pm

pcmanbob wrote:
Mon Apr 08, 2019 7:49 am
" Temporary failure in name resolution " would suggest that when you are running the program you do not have access to the internet.

Do you have internet access can you browse the web ?

If you run the script manually after your pi has booted does it work then ?

Thank you Bob,

I currently do have internet. My Raspberry Pi uses a wifi connection to access the internet. After I boot my pi I can run the script manually on my pi without any problems.

User avatar
scruss
Posts: 3082
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Why am I getting a name resolution error with my cron job?

Mon Apr 08, 2019 3:48 pm

Are you running this as a @reboot clause in your crontab? If so, it'll run long before the system has a wifi connection.

There are a couple of ways around this:
  1. put a sleep 30 ; before your command in the crontab entry. This is a simplistic way of waiting for the network; or
  2. enable wait for network in raspi-config in its boot options; or
  3. use systemd, and write a service file that includes a After=network-online.target directive, as in here: viewtopic.php?p=1439963#p1441713
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
B.Goode
Posts: 9886
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Why am I getting a name resolution error with my cron job?

Mon Apr 08, 2019 3:49 pm

auto514 wrote:
Mon Apr 08, 2019 3:32 pm
pcmanbob wrote:
Mon Apr 08, 2019 7:49 am
" Temporary failure in name resolution " would suggest that when you are running the program you do not have access to the internet.

Do you have internet access can you browse the web ?

If you run the script manually after your pi has booted does it work then ?

Thank you Bob,

I currently do have internet. My Raspberry Pi uses a wifi connection to access the internet. After I boot my pi I can run the script manually on my pi without any problems.

You have network access currently.

What @pcmanbob wrote was
suggest that when you are running the program you do not have access to the internet.
Maybe the Network is not ready when Cron runs that task at reboot?

There via a Wait for Network option in the raspi-config utility that might help?

auto514
Posts: 5
Joined: Thu Mar 28, 2019 5:24 pm

Re: Why am I getting a name resolution error with my cron job?

Mon Apr 08, 2019 5:52 pm

scruss wrote:
Mon Apr 08, 2019 3:48 pm
Are you running this as a @reboot clause in your crontab? If so, it'll run long before the system has a wifi connection.

There are a couple of ways around this:
  1. put a sleep 30 ; before your command in the crontab entry. This is a simplistic way of waiting for the network; or
  2. enable wait for network in raspi-config in its boot options; or
  3. use systemd, and write a service file that includes a After=network-online.target directive, as in here: viewtopic.php?p=1439963#p1441713
I was thinking about what the problem might be, and this was something I considered. I added a sleep to my script then rebooted my pi and it works. I think I will use the second option, though. It is less of a hassle.

Thanks!

Return to “Troubleshooting”