topkecleon / telegram-bot-bash

Telegram bot written in bash
Other
431 stars 123 forks source link

How to autostart bot? #200

Closed Azat868 closed 1 year ago

Azat868 commented 1 year ago

Im install via root ./bashbot.sh init and start from user ./bashbot.sh start But after reboot bot not started...im manually started

And second problem, after errors bot stopping, example error: Sun Apr 2 18:14:08 2023: RESULT=false FUNC=sendJson (curl) CHAT[ID]=566666666 ERROR=999 DESC=Send to telegram not possible, timeout/broken/no connection ACTION="text": How to fix autostart after errors?

gnadelwartz commented 1 year ago

Hello @Azat868

your questions inicates you are not so familar with bash and linux in general, therefore I can give you some hints:

gnadelwartz commented 1 year ago

regarding your second question, bashbot.sh will not stop running if e.g. sending a message fails, it reports the error and continue to watch for new commands from telegram.

The error provided indicates that your are probably using a script that was'nt able to connect to telegram for sending a message (probably one of the scripts in bin?). This indicates a misconfiguration or not correct use in standalone script.

To find more information about whats wrong in the log files start bash bot with "debug" as second argument.

Azat868 commented 1 year ago

Every day the bot automatically falls asleep. You have to connect and run manually on behalf of the limited user from which you installed it. ./commands.sh: line 22: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8): No such file or directory ./commands.sh: line 22: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8) ./mycommands.conf: line 21: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8) ./mycommands.conf: line 21: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)

gnadelwartz commented 1 year ago

Looks like your Linux has no locale C.UTF-8 installed or its named different, see https://github.com/topkecleon/telegram-bot-bash/blob/master/doc/4_expert.md#known-locale-pitfalls

you can check which locales are installed using the command locale -a and change it in mycommands accordingly

regarding falling asleep I have no concrete idea, but as a workaround you can restart your bot daily using bashbot.rc restart in cron https://github.com/topkecleon/telegram-bot-bash/blob/master/doc/4_expert.md#schedule-bashbot-from-cron

e.g. enter the following entry in your users crontab

# run every day at 0:00
  0 0 * * *  /usr/local/telegram-bot-bash/bashbot.rc restart    # restart bot

or in your system crontab for your specific user

# run every day at 0:00
  0 0 * * *  nobody /usr/local/telegram-bot-bash/bashbot.rc restart # restart bot

https://github.com/topkecleon/telegram-bot-bash/blob/master/examples/bashbot.cron

Azat868 commented 1 year ago

@gnadelwartz A big thank you my friend I have one more problem ... how to load variables from mycommands.sh into question.sh? Im try: if [ "${res}" -gt 0 ] ; then startproc "examples/question.sh" || send_normal_message "${CHAT[ID]}" "Can't start question." else send_normal_message "${CHAT[ID]}" "${MESSAGE} already running ..." fi

And inside question.sh:
getJson "${FILEURL}/${photo}" > $USER_FILE and another variables not working...

gnadelwartz commented 1 year ago

short answer: it's not possible to share current variable values between shell processes, this is the basic rule for shell processes conneted by a pipe.

You must do all processing in mycommands yourself or switch to a language which can share global variables between threads, e.g. async funtions in python.

gnadelwartz commented 1 year ago

As this may a fundamental misunderstanding on how interactive scripts work in bashbot, I added a clarification to the documentation.

https://github.com/topkecleon/telegram-bot-bash/blob/master/doc/3_advanced.md#interactive-chats

Thanks for pointing on the topic

Azat868 commented 1 year ago

First problem autostart(

0 0 * * *  /usr/local/telegram-bot-bash/bashbot.rc restart  # restart bot
Available commands: -h  help  init  start  stop  status  suspendback  resumeback  killback

Im tried:

#!/bin/bash
while true; do
 if ps ax | grep -v grep | grep "telegrambotname" > /dev/null 2>&1; 
 then 
 echo "Bot is running."; 
 else echo "Bot is not running. Starting..."; 
 su ter -c "/home/ter/telegram-bot-bash/bashbot.sh start"; 
 fi
  sleep 10  
done

But its not working correctly. The fact is that parameters -c or sudo -u run the script on behalf of user ter. but unfortunately after such a launch, ${URLS[PHOTO]} does not work correctly inside the bot (for some reason they are empty, but if you run the bot from the terminal, everything works). Therefore, I am looking for a solution to launch a bot using a unit, not a cron

The second problem is when a lot of users use the bot and my 4 CPU cores can't handle the influx. Of course, Linux kills any scripts, as it sees this as a big problem. I do some image processing in the bot, so I want to limit the bot's use of cores. I tried in limits.conf:

ter hard cpu 2
ter hard nice -19
ter hard as 500000
ter hard rss 5000000

But unfortunately it didn't work properly. Parameter -cpu restricts scripts to run as user ter in minutes and not in cores

Next, I resorted to utilities ulimit and cpulimit inside commands.sh:

ulimit -m 4500000 
cpulimit -l 75 -p $$ & 

This also didn't work. I'm also looking for a solution on how to limit the % load of cores, or just the number of cores. Sorry for so many questions. I'm just trying to eliminate the influx of users on the bot and its premature stop and start

Azat868 commented 1 year ago

I can also try to put heavy commands separately in a file and try to control them with cpulimit and ulimit, but unfortunately interactive chats(scripts/interactive.sh.clean) do not support: send_file "${CHAT[ID]}" "$OUTPUT_FILE1" "1" "document" So I'm a little confused

gnadelwartz commented 1 year ago

I strongly suggest to use "bashbot.'rc" instead of bashbot.sh" !!

bashbot.rc is excactly designed to run bashbot on system start, from cron, check if bashbot is running and so on. its explicitly designed to run bashbot as other user, from root with cron ... and can be used with systemd unit definition.

you must only check which sudo runuser etc command is avalible on your system and edit bashbot.rc definitions acordingly

gnadelwartz commented 1 year ago

Interactive chats was never meant for other stuff then sending and recieving TEXT only.

As explained in documentation now, its impossible to use bashbot functions and variables in interactive scripts: https://github.com/topkecleon/telegram-bot-bash/blob/master/doc/3_advanced.md#interactive-chats

gnadelwartz commented 1 year ago

A last tip for now: to limit the massive amount of parallel processes - and thus avoid killing processes because of overload - remove the "&" at the end of the following line in processUpdates.sh

https://github.com/topkecleon/telegram-bot-bash/blob/1c2c07397a3b01975ef7cfd936307ccd90ed60fb/modules/processUpdates.sh#L108

bashbot is meant for simple use cases like sending messages from bash scripts or process simple commands. As far I understand your use case I would suggest to use a more modern and specialised Telegram framework in e.g. Phyton