Force Glitch to restart regularly


#1

I’m creating a python Discord bot that needs to be restarted at least every day automatically. Is it possible to do in Glitch?


#2

Here’s the solution I came up with to solve your problem.

  1. Install cron, this is like cron jobs, but using timeouts
  2. Setup a cron to execute this line every 24 hours in your server process.exit(1);
  3. The cron will run, the app will shutdown and Glitch will bring it up again automatically

I tested it using a route in express and worked like a charm:

server.js

app.get("/shutdown", function (req, res) {
   process.exit(1);
});

#3

Except I’m using Python, not JSON or JS. Can this be done in the watch.json file?


#4

I don’t think so, but you can try for a similar solution in python using python-crontab and sys.exit().


#5

Hi Trainmaster2!

Glitch automatically restarts your app when it closes. So you just need to call sys.exit() somehow every 24 hours.

@kinduff why did you need forever? Glitch should be restarting your app when it dies.


#6

Looks like I’m less patient when it comes to restarts :sweat_smile: I tested too fast. That explains a thing or two. Gonna edit my answer above to avoid confusions.


#7

Figured out that if start.sh ends it will restart. Made the python code run in the background and made start.sh wait until midnight to terminate.

My start.sh looks like:

#!/bin/bash

# Exit early on errors
set -eu

# Python buffers stdout. Without this, you won't see what you "print" in the Activity Logs
export PYTHONUNBUFFERED=true

# Install Python 3 virtual env
VIRTUALENV=.data/venv

if [ ! -d $VIRTUALENV ]; then
  python3 -m venv $VIRTUALENV
fi

if [ ! -f $VIRTUALENV/bin/pip ]; then
  curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | $VIRTUALENV/bin/python
fi

# Install the requirements
$VIRTUALENV/bin/pip install -r requirements.txt

# Run a glorious Python 3 server
$VIRTUALENV/bin/python3 server.py &
while [ $(date +%H:%M) != "00:00" ]; do sleep 1; done