Rakesh
Rakesh

Reputation: 82785

[Errno 32]Broken pipe in Python Script in Crontab

I am running a Python script which downloads PDF files from the FTP. The script when run manually works perfectly but when i put it in crontab in my ubuntu machine and execute it i get a an error [Errno 32] Broken pipe. Any idea why this happens and how do i handle this?

Upvotes: 2

Views: 1433

Answers (2)

Bim
Bim

Reputation: 531

Just solved a Broken Pipe(32) error I was having, you can get the error when using ftplib globally & inside functions:

import ftplib
from time import sleep
ftp_handle = ftplib.FTP(host,user,pass)
ftp_handle.encoding = 'utf-8'
def func1():
    ftp_handle = ftplib.FTP(host,user,pass)
    ftp_handle.encoding = 'utf-8'
    ftp_handle.nlst()
    ftp_handle.quit()
while(1):
    now = datetime.now()
    print(now)
    try:
        func1()
    except Exception as e:
        print(e)
    sleep(10)

Inside the function it uses a local variable so creates another FTP instance, after some time, depending on the timeout settng of the FTP server, the global FTP instance times out and the local variable gives a broken pipe(32) error all you need to do is declare the ftp_handle as global inside the function

import ftplib
from time import sleep
ftp_handle = ftplib.FTP(host,user,pass)
ftp_handle.encoding = 'utf-8'
def func1():
    global ftp_handle
    ftp_handle = ftplib.FTP(host,user,pass)
    ftp_handle.encoding = 'utf-8'
    ftp_handle.nlst()
    ftp_handle.quit()
while(1):
    now = datetime.now()
    print(now)
    try:
        func1()
    except Exception as e:
        print(e)
    sleep(10)
    

This makes the function use the global scope so the FTP is not left open to timeout. Must be a bug that the local variable begins to error as it should be dis-related to the global variable.

Maybe this solves this ancient question?

Upvotes: 0

Rakesh
Rakesh

Reputation: 82785

Hi I dnt know y the error occurs but when i directed the print statements from my script to another file this error did not come and my script ran successfully Example: Myscript.py > test.log

Upvotes: 1

Related Questions