0

I have an annoucement bot and it would be a waste to run it all day for 1 announcement. So I am trying to schedule the start of server, start my bot, which on start anounces what it needs to and then schedule the shutdown 5 minutes later when it is surely done. I have installed the needed libraries and copied the code to the VM. If I try running the normal way by typing "python3 mybot.py" it works. I have also tried running it in tmux and it works in there as well. The problem is that I can't start it with the start up script. I don't know much bash so, so far I have tried:

  1. just moving to the folder and executing it normaly
#! /bin/bash
cd home/user
python3 bot.py
  1. moving to the folder and executing via a local bash file inside which was the command
#! /bin/bash
cd home/user
chmod +x run.sh
./run.sh
  1. starting the tmux session the same way and running the bot, but when I connected with ssh it wasnt on the list of active sessions.

I have also tried creating folder to check if it even executes and if I am at the right place and the folders did show up where I intended. Although it seems like it tries to execute the code in root despite creating the file at the place I moved to and finish the correct bash file. Also it might be because its a bot so its a continous code not like a script? I have also checked similar posts but couldn't understand fully/get them to work.

Any help is appreciated.

1 Answer 1

1

Updated answer

I think in your case running it on GCE might be better than my suggested GAE

In that case you have several options, but these 2 would be my advice:

  1. Create a script such as startup.sh. Save the file in your /etc/init.d/ directory. Change the permissions of the script (to make it executable): chmod +x /etc/init.d/mystartup.sh. Have this script call your py file.

  2. Edit the /rc.local script. In Ubuntu, this file is located in /etc/rc.local. You'll need to use sudo to edit this one. Put python3 path/to/file/bot.py at the bottom of this file and it will execute whenever it boots. To get the exact path, navigate to it and type pwd. You might need to doublecheck if your bot.py still can load whatever you're loading.

If you want to shut down the VM at the end of the bot.py, you can add this at the end of the py file:

import os
os.system('shutdown')

This will give you the standard 60 seconds to stop the shutdown. (It just executes the shell shutdown command)
Automatic shutdown can be a bit of a bother if you don't have enough time to cancel the shutdown...


Original answer

Depending on what your bot actually does (announcement is not very descriptive) you might be better off using the free tier App Engine, combined with a cron job that executes your python at the set times.

Throw a simple flask app in front to handle the incoming cron job request.

Also, block everything but requests from cronjobs on the GAE firewall to ensure only the cronjob can get through. To be absolutely sure the request comes from your GAE cronjob, you can check if the correct header is present.

main.py

from flask import Flask

app = Flask(__name__)

@app.route('/my/awesome/cronjob')
def my_bot_code():
    # your code goes here, or you can simply import & call your bot.py from here
    return 'im a bot'

app.yaml

runtime: python39

instance_class: F1
entrypoint: gunicorn -b :$PORT main:app

automatic_scaling:
  max_instances: 1

handlers:
- url: /.*
  secure: always
  redirect_http_response_code: 301
  script: auto

cron.yaml

cron:
- description: "daily summary job"
  url: /my/awesome/cronjob
  schedule: every day 09:00
Sign up to request clarification or add additional context in comments.

3 Comments

The bot at start reads a file in the same folder of events and checks it with the current date. If the date matches it announces the event in the given channel. It also has commands to add delete commands but since everyone just lets me do it I figured its fine if its shut down and it only announces the scheduled announcements.
due to the fact that you're dealing with local files, which I guess get updated somehow, I don't think GAE works for you. I've updated my answer with 2 ways to run scripts on VM startup...
Thank you for your answer, I didnt try it out although it did give me an idea to try a simpler version of your answer, where I just set a crontab, which executes a comand every 15 minutes, and then over that added a schedule in google cloud interface which runs my instance once a day for 15 minutes. So it starts the instance, crontab runs my bot, and the scheduler shuts down the instance after 15 minutes. So didnt try your solution but you did help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.