How to run a bash script while Ubuntu is starting up on tty

My grub doesn't have "quiet splash" so it displays text while booting, and I want to run "neofetch -L" just before it displays "Welcome to Ubuntu 20.04 LTS"


You can add a custom service to run your script at boot by following the steps below.

Firstly, run the following command in the terminal to create and edit a shell script file in your home directory:

nano ~/MyScript.sh

Secondly, copy and paste the following code into the editor, ( neofetch -L is just an example ) and save it by pressing Ctrl + X then press Y then press Enter :

#!/bin/bash

neofetch -L

Thirdly, make the shell script file executable by running the following command in the terminal:

chmod +x ~/MyScript.sh

Fourthly, create and edit a custom systemd service to run the shell script at boot by running the following command in the terminal:

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

Fifthly, copy and paste the following code into the editor, replace USERNAME with your username and save it by pressing Ctrl + X then press Y then press Enter :

[Unit]
Description=My Custom Service
Before=motd-news.service

[Service]
Type=oneshot
ExecStart=/home/USERNAME/MyScript.sh
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target

Sixthly, start the service by running the following command in the terminal:

sudo systemctl start MyCustomService

Seventhly, enable the service by running the following command in the terminal:

sudo systemctl enable MyCustomService

Finally, reboot your system.


If for any reason you want to undo this solution, please run the following commands in the terminal one after the other in the same sequence below:

  1. sudo systemctl stop MyCustomService
  2. sudo systemctl disable MyCustomService
  3. sudo rm /etc/systemd/system/MyCustomService.service
  4. rm ~/MyScript.sh

Notice:

  • neofetch must be installed on your system in order for the above example script to work. It can be installed like so:

    sudo apt install neofetch
    
  • StandardOutput=journal+console will enable printing output from your script on screen.

  • You can change when the custom service starts executing during boot by specifying in the service file under [Unit] the Before= and After= system services so that your custom service will run after and / or before certain system services. Please refer to this answer to list the system services in the order they are run.

Becareful:

Test your script first and make sure it executes in a timely correct way to avoid delay or lock-up during boot.