Automatically starting a Discord bot on Raspberry Pi B+ at boot

I’m new to Raspberry Pi and I’m trying to get my Discord bot to start automatically when the Pi boots up. I’ve been using Crontab -e to run my Python script, but it’s not working. Here’s what I’ve got in crontab:

@reboot sleep 60 && python3 /home/pi/Desktop/MiniWeston/Mini_Weston2.py &

I added the sleep command because I thought it might be an internet connection issue, but it still doesn’t work. I’ve looked online for solutions, but I’m having trouble understanding them. Can anyone help me figure out what I’m doing wrong? I’m pretty stuck and would really appreciate some guidance on how to get my bot running at startup. Thanks!

I eventually solved a similar problem by creating a simple shell script and invoking it from rc.local. I wrote a script (for example, named start_discord_bot.sh) that begins with a shebang (#!/bin/sh) and then runs the Python command to launch the bot. After making sure the script was executable with chmod +x, I edited /etc/rc.local right before the exit command to run the script as the appropriate user (using su pi -c ‘path/to/script &’). This method reliably waits for network services and avoids potential issues with timing or environment inconsistencies. It also simplifies troubleshooting because you can add logging directly in your script if needed.

I’ve had similar issues with autostarting scripts on Pi. One thing that helped me was using a bash script instead of directly calling the Python file. Create a script like ‘start_bot.sh’ with:

#!/bin/bash
cd /home/pi/Desktop/MiniWeston
python3 Mini_Weston2.py

Make it executable with ‘chmod +x start_bot.sh’. Then modify your crontab to call this script instead. This ensures the correct working directory and environment. Also, check your bot’s logs for any error messages - they might provide clues about why it’s not starting properly. If all else fails, systemd is indeed a more robust option as suggested earlier.

hey there! have u tried using systemd instead of crontab? it’s generally more reliable for running services at boot. create a .service file in /etc/systemd/system/ with ur bot’s details, then enable it with systemctl. might be easier than fiddling with crontab. lmk if u need help setting it up!