Cathy
Cathy

Reputation: 377

how to keep running a client program in python which uses Twilio

I have deployed a Flask application on an Ubuntu server. In order to make a check on the Flask application, I have used Twilio, such that the data will be sent to the server from the client every 5 minutes. In case something goes wrong, I should be getting a text message on my phone. Right now I am doing this on my local machine but I want to know how can I make it run always? Do I have to run the below client code on the Ubuntu server or how it could be done?

import json
import requests

def localClient():
    try:
        data = {"inputData": "Bank of America", "dataId": 12345}
        response = requests.post("http://12.345.567.890/inputData", json=data).json()
    except:
        from twilio.rest import Client
        account_sid = "XXXXXXXXXXXXXXX"
        auth_token = "XXXXXXXXX"
        client = Client(account_sid, auth_token)

        message = client.messages \
            .create(
                body='Server is down',
                from_='+12345678901',
                to='+19876543210' )
while True:
    localClient()
    time.sleep(300)

Upvotes: 1

Views: 70

Answers (1)

Prashant Suthar
Prashant Suthar

Reputation: 302

Use supervisor in Ubuntu. This will auto restart your code whenever you restart server. You don't need to start every time. This will run forever until you stop manually.

Refer to the following link to setup supervisor : supervisor

Upvotes: 1

Related Questions