Ubuntu Server 24.04 Automatic Login

Less clutter version:
I set up my Ubuntu Server with a log-in using RPi Imager. I have a Python Discord Bot that is running automatically (after I log-in), but the log-in is still present, and I do not know how to bypass it.

I need help finding a way to make my log-in automatic rather than manual, and yes, this isn’t going to be used 24/7.

As per what was mentioned, I added this to my .bashrc file to automatically start the Bot after log-in.

cd environments/Psylar
source my_env/bin/activate
python3 psylar.py

Asked By: Opressive Extenz

||

This answer does not solve the "automatically login to a Ubuntu server" problem.

This answer shows a way to start the python discord bot script when the computer boots.

Create a bash script

Create a folder bin within your home folder such as /home/$USER/bin. Then create a bash file called /home/$USER/bin/start_my_discord_bot.

#!/bin/bash
cd environments/Psylar
source my_env/bin/activate
python3 psylar.py

Since we are going to use it in cron, which has limited a path variable. You will need to edit the script above to include full path everywhere. For example, instead of python3 use /usr/bin/python3 or the correct path for python3 for the environment you created.

Make the bash script executable with the command:

chmod a+x /home/$USER/bin/start_my_discord_bot

Try using the command start_my_discord_bot and see if it works manually. We will make it automatic in the next steps.

I have provided 2 options. You only need one. Try the other option if the first does not work. Before trying the second option, make sure the bot is not running and the changes to the crontab made in option 1 is reversed.

Option 1: Crontab

Use the crontab -e to create a new crontab file. At the end of the file add the line:

@reboot /home/$USER/bin/start_my_discord_bot

Save and exit the editor.

When you restart the computer your discord bot should start automatically.

Option 2: Systemd

Systemd can do the same thing crontab does. It is a more modern and flexible approach:

Created a service using:

sudo nano /etc/systemd/system/start_my_discord_bot.service

and place the following lines this inside it:

[Unit]
Description=Start my discord bot
[Service]
User=XXX # REPLACE XXX BY YOUR USERNAME

#change this to your workspace
WorkingDirectory=/home/XXX # REPLACE XXX BY YOUR USERNAME

#path to executable. 
#executable is a bash script which calls jar file
ExecStart=/home/XXX/bin/start_my_discord_bot

SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

Save the file and exit the editor. Make sure the bot is not running already. Then use the following commands to enable the and start the service:

sudo systemctl daemon-reload
sudo systemctl enable --now start_my_discord_bot.service
sudo systemctl status start_my_discord_bot.service

The last command will show if the start_my_discord_bot.service is active or not. If it is not active, something has gone wrong. If it is active, check that the bot is running.

After a reboot the service should start automatically.

Hope this helps

Answered By: user68186
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.