How to run a discord bot on startup
PUBLISHED: Wed Sep 09 2020 00:00:00 GMT+0000 (Coordinated Universal Time)We all know how annoying it is if you restarted your server and you now need to go and manually run your bot each time you restart or shutdown the VPS, luckily there is a way to auto start your program so you don't have to do it manually ever again!
Jumplinks:
Based on the operating system, different methods are used, jump to the operating system your server/VPS uses-
1. Linux
2. Windows
3. Raspberry pi
Linux: Using systemd
The programs that are launched at startup are controlled by systemd, the system and service manager. systemd is the first process to run at startup. It will always has a process of ID (PID) 1. Every other process running in your computer is started by systemd, or by a process that systemd has already started.
follow the steps given below to auto start a service using systemd
:
Step 1. Creating a service
First of all you will need to create a service, this defies the service and how it will interact with the operating system.
The example given below for Ubuntu as Ubuntu is among of the most popular linux distros, but it should apply to almost every linux distros out there
You will need to create a file in /etc/systemd/system/
Use sudo cat> /etc/systemd/system/MyService.service
to create your service, replace "MyService" with the name you prefer
then copy past the following:
[Unit]
Description = My discord bot
After network.target = auditd.service
[Service]
Type =
ExecStart = / usr / local / bin / start myBot.js
ExecStop = / usr / local / bin / stop myBot.js
ExecReload = / usr / local / bin / reload myBot.js
[Install]
WantedBy = multi-user.target
Replace all instances of "myBot.js" with your main file name (this could of any format .sh, .py etc)
Step 2. Restarting systemctl daemon
We now need to reload the list of services runsudo systemctl daemon reload
Then activate the launch of the service at boot using sudo systemctl enable MyService
And boom! you are finished with it, to see the status anytime use sudo systemctl status MyService
Windows
Follow the steps below to run a program on startup in windows
1. Open startup apps folder
Windows has a special folder that when windows boots up runs all the application located inside this folder, to locate and open the folder follow the steps given below
1. Press Windows key + R button to open up Run prompt
2. Copy paste Shell:startup
and press enter
and you should be presented with a folder located at somewhere like C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
this is where start up programs are located.
2. Create a bash script
You can use a batch script / VBScript to launch your Python file. I used a bat file or batch file to do so. Make a new directory and create launcher.vbs here. It should look something like this
now place the given text into your newly created "launcher.bat" file-
For python
@echo off
"locationToPython/python.exe" "locationToYourPyFile\main.py"
pause
now you will need to replace "locationToPython/python.exe" with the location of python on you pc and "locationToYourPyFile\main.py" with the location of the python file, an example is given below
@echo off
"C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe" "C:\Users\Nihal\Desktop\main.py"
pause
Now add a short cut for launcher.bat
to >C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
and boom you are done! Your python file should auto start when the os boots up
For node
Follow the following steps if you use node
1. Create file called launcher.vbs
create a small Visual Basic Script called launcher.vbs that will start your node and paste the following
CreateObject("Wscript.Shell").Run "node app.js", 0
2. add to startup
To execute it automatically at startup, open the %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\
directory and add a shortcut to the launcher.vbs file.
and boom you are done! Your python file should auto start when the os boots up
For raspberry pi os/Raspbian
Now ikik that raspbian is just a distro based on Debian, but since many (including myself) use a raspberry pi to host discord bot I thought it would be worth to show how to specifically do it for raspberry pi os/raspbian.
1. Creating a service
On your system running raspbian, create a .service file for your service this can be any location:
[Unit]
Description=A service to autostart d.py bot.
After=network.target
[Service]
ExecStart=/usr/bin/python3 -u Main.py
WorkingDirectory=/home/pi/Desktop/Python-Bot
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
So in the example provided above, the service will use Python 3 from our working directory /home/pi/Desktop/Python-Bot
which contains my main file, remember you can change it to simply change the ExecStart line to be the command to start any program/script that you want running from booting and is not limited to python files only.
2. Copy to path
Now Copy this file into /etc/systemd/system
as root, using the command sudo cp launcher.service /etc/systemd/system/launcher.service
3. Restart systemctl daemon
Once this has been copied, you will have to restart/reload systemd to register the new service has been added. This is done with the following command: sudo systemctl daemon-reload
4. Start service
Now start the service we just created using the following command: sudo systemctl start launcher.service
Running that should start your bot, but we are not done yet, we need to make the service start at boot head to the next section
5. Run service on boot
Enable the service using the following commandsudo systemctl enable launcher.service
You can always see the status of your service using systemctl status launcher.service
and boom the service should now auto start when raspberry pi boots up! 😄 Do share the article if you found it helpful ❤️