Python script on Ubuntu 16.04 Webserver stops running after some time












0















I set up 3 Python Scripts for a telegram bot.



One is a lib which defines used methods:



import requests
import json
import configparser as cfg

class telegram_chatbot():

def __init__(self, config):
self.token = self.read_token_from_config_file(config)
self.base = "https://api.telegram.org/bot{}/".format(self.token)

def get_updates(self, offset=None):
url = self.base + "getUpdates?timeout=100"
if offset:
url = url + "&offset={}".format(offset + 1)
r = requests.get(url)
return json.loads(r.content)

def send_message(self, msg, chat_id):
url = self.base + "sendMessage?chat_id={}&text={}".format(chat_id, msg)
if msg is not None:
requests.get(url)

def read_token_from_config_file(self, config):
parser = cfg.ConfigParser()
parser.read(config)
return parser.get('creds', 'token')


The second one listens for user input and then responds a message:



from bot import telegram_chatbot
import csv

bot = telegram_chatbot("config.cfg")

print("bot started...")

def make_reply(msg):
reply = ""
if msg is not None and msg == "/naechsteaufgabe":
string = ", "
with open("Marketing-Plan.csv", "r") as csvfile:
reader = csv.reader(csvfile, delimiter = ";")
next(reader)
for row in reader:
if row[6] != "erledigt":
string = string.join(row)
reply = string
return reply

elif msg is not None and msg == "/hilfe":
reply = "text"
return reply

if msg is not None and msg == "/aufgaben":
with open("Marketing-Plan.csv", "r") as csvfile:
reader = csv.reader(csvfile, delimiter = ";")
next(reader)
list = [x for x in reader if x[6] == "mussnoch"]
string = ""
for entry in list:
string = string + " ".join(entry) + "n"
reply = string
if reply == "" or reply == None:
reply = "Error"
return reply
return reply


update_id = None
while True:
updates = bot.get_updates(offset=update_id)
updates = updates["result"]
if updates:
for item in updates:
update_id = item["update_id"]
try:
message = str(item["message"]["text"])
except:
message = None
from_ = item["message"]["chat"]["id"]
reply = make_reply(message)
bot.send_message(reply, from_)


and the third script should create a message and send it in a group every 24h:



import time
from bot import telegram_chatbot
import csv
bot = telegram_chatbot("config.cfg")

starttime=time.time()

def make_reminder():
with open("Marketing-Plan.csv", "r") as csvfile:
string = ", "
reader = csv.reader(csvfile, delimiter = ";")
next(reader)
for row in reader:
if row[6] != "erledigt":
string = string.join(row)
return string


while True:
bot.send_message(make_reminder(), "somechatid")
print("tick")
time.sleep(86400.0 - ((time.time() - starttime) % 86400.0))


These scripts run on my Ubuntu 16.04 webserver, but after some time (~24h) the scripts seem to stop, because the bot doesnt respond to user input anymore.



How and why does this happen? How do i change this so the bot works 24/7?










share|improve this question



























    0















    I set up 3 Python Scripts for a telegram bot.



    One is a lib which defines used methods:



    import requests
    import json
    import configparser as cfg

    class telegram_chatbot():

    def __init__(self, config):
    self.token = self.read_token_from_config_file(config)
    self.base = "https://api.telegram.org/bot{}/".format(self.token)

    def get_updates(self, offset=None):
    url = self.base + "getUpdates?timeout=100"
    if offset:
    url = url + "&offset={}".format(offset + 1)
    r = requests.get(url)
    return json.loads(r.content)

    def send_message(self, msg, chat_id):
    url = self.base + "sendMessage?chat_id={}&text={}".format(chat_id, msg)
    if msg is not None:
    requests.get(url)

    def read_token_from_config_file(self, config):
    parser = cfg.ConfigParser()
    parser.read(config)
    return parser.get('creds', 'token')


    The second one listens for user input and then responds a message:



    from bot import telegram_chatbot
    import csv

    bot = telegram_chatbot("config.cfg")

    print("bot started...")

    def make_reply(msg):
    reply = ""
    if msg is not None and msg == "/naechsteaufgabe":
    string = ", "
    with open("Marketing-Plan.csv", "r") as csvfile:
    reader = csv.reader(csvfile, delimiter = ";")
    next(reader)
    for row in reader:
    if row[6] != "erledigt":
    string = string.join(row)
    reply = string
    return reply

    elif msg is not None and msg == "/hilfe":
    reply = "text"
    return reply

    if msg is not None and msg == "/aufgaben":
    with open("Marketing-Plan.csv", "r") as csvfile:
    reader = csv.reader(csvfile, delimiter = ";")
    next(reader)
    list = [x for x in reader if x[6] == "mussnoch"]
    string = ""
    for entry in list:
    string = string + " ".join(entry) + "n"
    reply = string
    if reply == "" or reply == None:
    reply = "Error"
    return reply
    return reply


    update_id = None
    while True:
    updates = bot.get_updates(offset=update_id)
    updates = updates["result"]
    if updates:
    for item in updates:
    update_id = item["update_id"]
    try:
    message = str(item["message"]["text"])
    except:
    message = None
    from_ = item["message"]["chat"]["id"]
    reply = make_reply(message)
    bot.send_message(reply, from_)


    and the third script should create a message and send it in a group every 24h:



    import time
    from bot import telegram_chatbot
    import csv
    bot = telegram_chatbot("config.cfg")

    starttime=time.time()

    def make_reminder():
    with open("Marketing-Plan.csv", "r") as csvfile:
    string = ", "
    reader = csv.reader(csvfile, delimiter = ";")
    next(reader)
    for row in reader:
    if row[6] != "erledigt":
    string = string.join(row)
    return string


    while True:
    bot.send_message(make_reminder(), "somechatid")
    print("tick")
    time.sleep(86400.0 - ((time.time() - starttime) % 86400.0))


    These scripts run on my Ubuntu 16.04 webserver, but after some time (~24h) the scripts seem to stop, because the bot doesnt respond to user input anymore.



    How and why does this happen? How do i change this so the bot works 24/7?










    share|improve this question

























      0












      0








      0








      I set up 3 Python Scripts for a telegram bot.



      One is a lib which defines used methods:



      import requests
      import json
      import configparser as cfg

      class telegram_chatbot():

      def __init__(self, config):
      self.token = self.read_token_from_config_file(config)
      self.base = "https://api.telegram.org/bot{}/".format(self.token)

      def get_updates(self, offset=None):
      url = self.base + "getUpdates?timeout=100"
      if offset:
      url = url + "&offset={}".format(offset + 1)
      r = requests.get(url)
      return json.loads(r.content)

      def send_message(self, msg, chat_id):
      url = self.base + "sendMessage?chat_id={}&text={}".format(chat_id, msg)
      if msg is not None:
      requests.get(url)

      def read_token_from_config_file(self, config):
      parser = cfg.ConfigParser()
      parser.read(config)
      return parser.get('creds', 'token')


      The second one listens for user input and then responds a message:



      from bot import telegram_chatbot
      import csv

      bot = telegram_chatbot("config.cfg")

      print("bot started...")

      def make_reply(msg):
      reply = ""
      if msg is not None and msg == "/naechsteaufgabe":
      string = ", "
      with open("Marketing-Plan.csv", "r") as csvfile:
      reader = csv.reader(csvfile, delimiter = ";")
      next(reader)
      for row in reader:
      if row[6] != "erledigt":
      string = string.join(row)
      reply = string
      return reply

      elif msg is not None and msg == "/hilfe":
      reply = "text"
      return reply

      if msg is not None and msg == "/aufgaben":
      with open("Marketing-Plan.csv", "r") as csvfile:
      reader = csv.reader(csvfile, delimiter = ";")
      next(reader)
      list = [x for x in reader if x[6] == "mussnoch"]
      string = ""
      for entry in list:
      string = string + " ".join(entry) + "n"
      reply = string
      if reply == "" or reply == None:
      reply = "Error"
      return reply
      return reply


      update_id = None
      while True:
      updates = bot.get_updates(offset=update_id)
      updates = updates["result"]
      if updates:
      for item in updates:
      update_id = item["update_id"]
      try:
      message = str(item["message"]["text"])
      except:
      message = None
      from_ = item["message"]["chat"]["id"]
      reply = make_reply(message)
      bot.send_message(reply, from_)


      and the third script should create a message and send it in a group every 24h:



      import time
      from bot import telegram_chatbot
      import csv
      bot = telegram_chatbot("config.cfg")

      starttime=time.time()

      def make_reminder():
      with open("Marketing-Plan.csv", "r") as csvfile:
      string = ", "
      reader = csv.reader(csvfile, delimiter = ";")
      next(reader)
      for row in reader:
      if row[6] != "erledigt":
      string = string.join(row)
      return string


      while True:
      bot.send_message(make_reminder(), "somechatid")
      print("tick")
      time.sleep(86400.0 - ((time.time() - starttime) % 86400.0))


      These scripts run on my Ubuntu 16.04 webserver, but after some time (~24h) the scripts seem to stop, because the bot doesnt respond to user input anymore.



      How and why does this happen? How do i change this so the bot works 24/7?










      share|improve this question














      I set up 3 Python Scripts for a telegram bot.



      One is a lib which defines used methods:



      import requests
      import json
      import configparser as cfg

      class telegram_chatbot():

      def __init__(self, config):
      self.token = self.read_token_from_config_file(config)
      self.base = "https://api.telegram.org/bot{}/".format(self.token)

      def get_updates(self, offset=None):
      url = self.base + "getUpdates?timeout=100"
      if offset:
      url = url + "&offset={}".format(offset + 1)
      r = requests.get(url)
      return json.loads(r.content)

      def send_message(self, msg, chat_id):
      url = self.base + "sendMessage?chat_id={}&text={}".format(chat_id, msg)
      if msg is not None:
      requests.get(url)

      def read_token_from_config_file(self, config):
      parser = cfg.ConfigParser()
      parser.read(config)
      return parser.get('creds', 'token')


      The second one listens for user input and then responds a message:



      from bot import telegram_chatbot
      import csv

      bot = telegram_chatbot("config.cfg")

      print("bot started...")

      def make_reply(msg):
      reply = ""
      if msg is not None and msg == "/naechsteaufgabe":
      string = ", "
      with open("Marketing-Plan.csv", "r") as csvfile:
      reader = csv.reader(csvfile, delimiter = ";")
      next(reader)
      for row in reader:
      if row[6] != "erledigt":
      string = string.join(row)
      reply = string
      return reply

      elif msg is not None and msg == "/hilfe":
      reply = "text"
      return reply

      if msg is not None and msg == "/aufgaben":
      with open("Marketing-Plan.csv", "r") as csvfile:
      reader = csv.reader(csvfile, delimiter = ";")
      next(reader)
      list = [x for x in reader if x[6] == "mussnoch"]
      string = ""
      for entry in list:
      string = string + " ".join(entry) + "n"
      reply = string
      if reply == "" or reply == None:
      reply = "Error"
      return reply
      return reply


      update_id = None
      while True:
      updates = bot.get_updates(offset=update_id)
      updates = updates["result"]
      if updates:
      for item in updates:
      update_id = item["update_id"]
      try:
      message = str(item["message"]["text"])
      except:
      message = None
      from_ = item["message"]["chat"]["id"]
      reply = make_reply(message)
      bot.send_message(reply, from_)


      and the third script should create a message and send it in a group every 24h:



      import time
      from bot import telegram_chatbot
      import csv
      bot = telegram_chatbot("config.cfg")

      starttime=time.time()

      def make_reminder():
      with open("Marketing-Plan.csv", "r") as csvfile:
      string = ", "
      reader = csv.reader(csvfile, delimiter = ";")
      next(reader)
      for row in reader:
      if row[6] != "erledigt":
      string = string.join(row)
      return string


      while True:
      bot.send_message(make_reminder(), "somechatid")
      print("tick")
      time.sleep(86400.0 - ((time.time() - starttime) % 86400.0))


      These scripts run on my Ubuntu 16.04 webserver, but after some time (~24h) the scripts seem to stop, because the bot doesnt respond to user input anymore.



      How and why does this happen? How do i change this so the bot works 24/7?







      python linux ubuntu-16.04






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 21:56









      Ingor SievazIngor Sievaz

      56




      56
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462703%2fpython-script-on-ubuntu-16-04-webserver-stops-running-after-some-time%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462703%2fpython-script-on-ubuntu-16-04-webserver-stops-running-after-some-time%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'