Report this

What is the reason for this report?

Send email or restart when a background python script gives an error

Posted on March 26, 2014

Hi, I am running a Python script that retrieves Twitter data (detached using screen). It sometimes gives the error “connection was forcibly closed by remote host”. Is it possible to automate sending an email or restart the script when it gives an error? The script is saving the output to a file and not printing anything on the screen, if that helps.

It runs on Ubuntu 12.04.3 x64

I am also attaching the script. Thank you.

class CustomStreamListener(tweepy.StreamListener): def on_status(self, status): if status.lang == “tr”: with codecs.open(os.path.join(_dir, ‘do.json’), “a”, ‘utf-8’) as textFile: textFile.write(status.json) textFile.write(‘\n’)

def on_error(self, status_code):
    print >> sys.stderr, 'Encountered error with status code:', status_code
    return True # Don't kill the stream

def on_timeout(self):
    print >> sys.stderr, 'Timeout...'
    return True # Don't kill the stream

if name == ‘main’: print “extracting” l = CustomStreamListener() auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret)

stream = tweepy.Stream(auth, l)
stream.filter(track = keyword)


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Have you checked if Twitter might be limiting your API queries?

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.