Setting up auto-restart using screen (Unix)

This is a quick and ugly way of ensuring the bot is running and restarts if it is not. I’m sure there are better ways of handling this out there, but this method has worked for me for over a year already.


This method uses a cronjob and screen.

How to install screen
$ apt-get install screen
How to access cronjob list
$ crontab -e

How it works

  1. A bash script is created that starts the bot in a named screen.

  2. A cronjob is created to run the bash script every minute under the same name.

    • If the screen name exists, that means the bot is already running.

That’s it.

Bash script
d=`date '+%Y-%m-%d %H:%M:%S'`
if ! screen -ls | grep -q "fjbot"; then
    echo "$d: Restarting DiscordBot"
    screen -X -S fjbot quit
    screen -d -m -S fjbot python3 ./fjbot/
    echo "$d: Already Running"


This creates the job that runs the script above every minute. Temporary logs are made to ensure they are working.

* * * * * /path/to/ >> /tmp/discordbot.log 2>&1 &