In this article you will learn how start run script ( python, bash etc. ) on startup on Ubuntu 20.04 Server/Desktop

In this tutorial you will learn:
  • How to create Systemd service unit
  • How to simple disk space check script
  • How to set permissions to Systemd service unit file
  • How to set script permissions
  • How to enable Systemd service unit to run at the system startup
run script on startup on Ubuntu 20.04 Focal Fossa KDE Server/Desktop
How to run script on startup on Ubuntu 20.04 Focal Fossa KDE Server/Desktop

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed or upgraded Ubuntu 20.04 Focal Fossa
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

How to run script on startup on Ubuntu 20.04 step by step instructions

The Ubuntu 20.04 is a based on Systemd hence the simplest and recommended way to run a script on startup is to create Systemd service file and execute any script such as bash, python etc, via this service during the system boot.

The below steps will show you how to run example bash script which reports disk space usage of /home directory and saves the report in the /root directory every time the Ubuntu system boots.


SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


  1. First, create a Systemd service file.The below is an example of such file:

    [Unit]
    After=network.service
    
    [Service]
    ExecStart=/usr/local/bin/disk-space-check.sh
    
    [Install]
    WantedBy=default.target
    
    After: Instructs systemd on when the script should be run. In our case the script will run after network connection. Other example could be mysql.target etc.
    ExecStart: This field provides a full path the actual script to be executed on startup
    WantedBy: Into what boot target the systemd unit should be installed
    NOTE
    For more information on how to create Systemd service unit execute the man systemd.unit command.
  2. Create a script to be executed on Ubuntu system startup. As specified in the above Step 1, the path and the name of the new script is /usr/local/bin/disk-space-check.sh.

    The below is an example of such script:
    #!/bin/bash
    
    date > /root/disk_space_report.txt
    du -sh /home/ >> /root/disk_space_report.txt
  3. Set appropriate permissions for both, the Systemd service unit and script:
    $ sudo chmod 744 /usr/local/bin/disk-space-check.sh
    $ sudo chmod 664 /etc/systemd/system/disk-space-check.service
    


  4. Next enable the service unit:
    $ sudo systemctl daemon-reload
    $ sudo systemctl enable disk-space-check.service
    
  5. Now you are ready to reboot your system. Once the system boots you should see the following file containing disk space usage within your /root directory:
    $ sudo ls /root/
    disk_space_report.txt
    
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.