Run Script on Boot Ubuntu 22 2

Run Script on Boot Ubuntu 22

Sometimes you need to boot or startup the system because of some malfunction or any other updates you want to add to your operating system. This requires script addition at the startup. There exists several methods that are used to execute the scripts and programs in Ubuntu. We have used the most common approach through systemd. First, we will check some requirements whether our system is ready for the boot or not. Once we get the green flag, then we move further.

First, we will check the CPU stability and whether the processor is in the state to boot or reboot the Ubuntu operating system. For this purpose, this simple command is utilized in the Linux terminal:

$ cpufreq-info

Run Script on Boot Ubuntu 22 3

This command depicts the frequency of CPU information. This information is quite beneficial as it shows the data regarding the errors that can occur before or during the boot process. Further, it describes the number of processors that are working at a time. This information also describes the drivers installed or active at that time.

If the above-cited command does not work for you, then it means you need to install first the CPU frequency utilities to your Linux operating system.

$ Sudo apt install cpufrequtils

Run Script on Boot Ubuntu 22 4

After the successful installations, you will then use the above command in the terminal to get the CPU information.

Besides CPU processing, another big factor that is responsible for providing essential information before the boot is memory reading. Boot process requires space and a required area in the memory blocks. Because of this, we should check the memory criteria before proceeding with the boot process. This memory sheet is obtained through a sudo command used with ‘-h’.

$ Sudo free -h

Run Script on Boot Ubuntu 22 5

As displayed above, the result of that command shows the total memory size, the part of the memory used in other processes taking place in the system. Also, the available free space is displayed and the amount of memory that is shared between more than two processes.

Now, we are assured of the memory space, which means there is enough space to carry out the startup script on the system.

Step 1: Systemd service unit

Now, we are coming towards the process of running a script at the time of boot. The initial step is to build a system service unit. The Ubuntu operating system relies on the systemd, as it is easily applied and also a recommended approach to run a code or script on boot or the startup of the system. So, the simplest method is to create a system service file plus execute the script code like bash through this created service when the system boots.

In the below step, we have run an example of the script written in bash language that provides the data regarding the home directory. Then, saved it to the /root directory whenever the system will boot to create a system file. Finally, it will be stored at a location that is mentioned via a path in the command.

$ Sudo nano /etc/systemd/system/my-startup.service

Run Script on Boot Ubuntu 22 6

This will first require the user credentials. Then, it will verify the user and you will see that the default interface of the terminal is changed into another interface. This is a blank page where you are suppose to create a service having three basic information.

Run Script on Boot Ubuntu 22 7

The three basic parameters you will mention here include:

  • Description: This part provides instructions to the systemd where the scripts should be executed. This description can be any value you want to apply. We have just given the file name.
  • ExecStart: This portion contains the full location path of the original script that is to be executed on boot.
  • WantedBy: This shows the targeted area, or you can say that the unit of system, the boot should target.

Step 2: Create a script

You can manually create a file in the file manager or create a new script file through the terminal and open it.

Go to the directory and generate a folder of scripts

$ mkdir scripts

After that, access the folder and then create a further bash file with the ‘.sh’ extension named ‘script. sh’ through the touch command.

$ cd scripts

Run Script on Boot Ubuntu 22 8

$ touch script. sh

Use any text to enter in the file and through the chmod command, you can display the text.

$ Chmod –R 777 .

$ ./

The text will be displayed. This was a sample text. But to create a script to be run at the time of boot, we will write the following code:

Run Script on Boot Ubuntu 22 9

This was a snippet of the bash file. Now to incorporate this file, we will use the instruction in the terminal.

$ Sudo nano /usr/local/sbin/

Run Script on Boot Ubuntu 22 10

Apply the password first and then the script will be applied.

Step 3: Set permissions

After the system unit service is created and the bash file is introduced, you need to set some permissions for both, so that there is no interruption while applying the boot process.

$ sudo chmod 744 /usr/local/bin/

$ sudo chmod 664 /etc/systemd/system/my-startup.service

Run Script on Boot Ubuntu 22 11

Run Script on Boot Ubuntu 22 12

Step 4: Enable the service unit

Once permission is given, you can enable the service unit and check the status of the unit whether it is working properly or not.

$ sudo systemctl enable my-startup.service

Run Script on Boot Ubuntu 22 13

You can see that a symlink is created at a multi-user installation with all the services.

After that, we can boot our system.

$ sudo ls /root/

Run Script on Boot Ubuntu 22 14


Boot process on Ubuntu requires a lot of verification and maintenance of the system that mainly includes disk check, memory or CPU processor check, etc. In this article, we have explained these checks in detail before proceeding with the boot process. The process is explained in 4 major steps. We took the start from the system service unit and created a bash file, that will lead to the permissions given to the unit and then enable the systemd service unit to boot the system.

Leave a Comment

Your email address will not be published. Required fields are marked *