Linux startup consists of several stages, during which you can configure a program to start automatically. This can be a single command, a sequence of commands, or an executable shell script. However, startup procedures may vary between different Linux distributions and versions.

Modern Linux systems boot into systemd, while older Linux versions utilize System V init. Regardless, both systems will execute cron and rc.local before loading the desktop environment, such as GNOME or KDE. In contrast, server-based Linux distributions do not load a desktop environment and instead provide a login prompt at the console. After logging in, the default shell like Bash runs.

Running a program automatically on Linux startup via systemd

systemd is the standard system and service manager in modern Linux, responsible for executing and managing programs during startup, among other tasks. Compatible programs will include service unit files used by systemd to manage the program's execution.

To configure systemd to run a program automatically during Linux startup, follow these steps:

  1. Check if the service unit for your program exists (optional).
    $ sudo systemctl list-unit-files --type=service
    [sudo] password for user:
    UNIT FILE                              STATE
    accounts-daemon.service                enabled
    apparmor.service                       enabled
    apport-autoreport.service              static
    apport-forward@.service                static
    apport.service                         generated
    apt-daily-upgrade.service              static
    apt-daily.service                      static
    atd.service                            enabled
    autovt@.service                        enabled
    blk-availability.service               enabled
    bootlogd.service                       masked
    bootlogs.service                       masked
    bootmisc.service                       masked
    checkfs.service                        masked
    checkroot-bootclean.service            masked
    checkroot.service                      masked
    cloud-config.service                   enabled
    cloud-final.service                    enabled
    cloud-init-local.service               enabled
    cloud-init.service                     enabled
    console-getty.service                  disabled
    ##### snipped #####

    You'll have to create your own service unit if it's a custom program or if your program doesn't come with one during installation
    Related: Creating and modifying systemd unit files

  2. Check if the service unit is enabled (optional).
    $ sudo systemctl is-enabled mysql
    disabled

    enabled service unit is executed during boot

  3. Enable the service unit to execute during startup.
    $ sudo systemctl enable mysql
    Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install.
    Executing: /lib/systemd/systemd-sysv-install enable mysql
    Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
  4. Confirm if the service unit is enabled (optional).
    $ sudo systemctl is-enabled mysql
    enabled

Running a program automatically on Linux startup via cron

cron is a daemon used to execute scheduled commands stored in the cron job table (crontab), which is unique for each user in the system. It starts during system boot, either via systemd or System V init, and you can schedule your program to run during system boot by following these steps:

  1. Open the default crontab editor.
    $ crontab -e

    You're required to select an editor for the crontab if this is the first time the user uses the command.

    $ crontab -e
    no crontab for user - using an empty one
    
    Select an editor.  To change later, run 'select-editor'.
      1. /bin/nano        <---- easiest
      2. /usr/bin/vim.basic
      3. /bin/ed
    
    Choose 1-3 [1]:

    A crontab will be created for the user running the command and will be executed using the privileges of the user. If you need your program to run as the root user, run crontab -e as the root user itself.

  2. Add a line beginning with @reboot.
    # m h  dom mon dow   command
    @reboot

    @reboot defines the job to be executed during system boot.

  3. After @reboot, insert the command to start your program.
    @reboot /sbin/ip addr | grep inet\ | tail -n1 | awk '{ print $2 }' > /etc/issue && echo "" >> /etc/issue

    Use full path for your programs when possible and write your commands in a single line.

  4. Save the file to install it to the crontab.
    $ crontab -e
    crontab: installing new crontab
    $ 

    The file is saved in /var/spool/crontab/<username>

  5. Verify proper crontab configuration (optional).
    $ crontab -l
    # m h  dom mon dow   command
    @reboot /sbin/ip addr | grep inet\ | tail -n1 | awk '{ print $2 }' > /etc/issue && echo "" >> /etc/issue

Running a program automatically on Linux startup via rc.local

rc.local is a legacy script from the System V init system, executed before displaying a login screen for the desktop environment or terminal login prompt. It is typically a Bash shell script capable of running any commands.

To configure your rc.local script, follow these steps:

  1. As the root user, open or create the /etc/rc.local file using your preferred editor if it doesn't exist.
    $ sudo vi /etc/rc.local
  2. Add placeholder code to the file.
    #!/bin/bash
    
    exit 0

    It must start with interpreter (/bin/bash) and ends with an exit code (0 is for success)

  3. Insert commands and logic as needed.
    #!/bin/bash
    
    /sbin/ip addr | grep inet\ | tail -n1 | awk '{ print $2 }' > /etc/issue
    echo "" >> /etc/issue
    
    exit 0
  4. Set the file to executable.
    $ sudo chmod a+x /etc/rc.local

    The file will be executed as the root user during system boot

Running a program automatically on GNOME startup

GNOME is the default desktop environment for Linux distributions like Ubuntu and Red Hat. You can configure GNOME to run programs upon user login by following the instructions in the linked article:

Running a program automatically on KDE startup

KDE is another popular Linux desktop environment and the default for Kubuntu and openSUSE. It can also be configured to run programs when a user logs in, as detailed in the related article:

Running a program automatically on new Bash session

A new shell program will be spawned when you start your terminal session. Bash is the default shell for most Linux distributions, and when started, it will look for the following files in the particular starting a terminal session, a new shell program will be spawned. Bash is the default shell for most Linux distributions and, when initiated, looks for and executes the following files in order:

  /etc/profile
  ~/.bash_profile
  ~/.bash_login
  ~/.profile

These files contain commands and logic for setting up environment variables and running required programs in the Bash language. They are also typically configured to execute other files, such as /etc/bashrc, /etc/bash.bashrc, and ~/.bashrc.

You can edit any of these files to run your program when a Bash session is started. Below is a part of a typical ~/.bashrc file:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \$ '
 
PATH=/home/user/bin:$PATH
 
export EDITOR=/usr/bin/vim
 
alias ll="ls -l"
Discuss the article:

Comment anonymously. Login not required.