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.
Methods to automatically run program on Linux startup:
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:
$ 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
$ sudo systemctl is-enabled mysql disabled
enabled service unit is executed during boot
$ 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.
$ sudo systemctl is-enabled mysql enabled
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:
$ 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.
# m h dom mon dow command @reboot
@reboot defines the job to be executed during system boot.
@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.
$ crontab -e crontab: installing new crontab $
The file is saved in /var/spool/crontab/<username>
$ crontab -l # m h dom mon dow command @reboot /sbin/ip addr | grep inet\ | tail -n1 | awk '{ print $2 }' > /etc/issue && echo "" >> /etc/issue
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:
$ sudo vi /etc/rc.local
#!/bin/bash exit 0
It must start with interpreter (/bin/bash) and ends with an exit code (0 is for success)
#!/bin/bash /sbin/ip addr | grep inet\ | tail -n1 | awk '{ print $2 }' > /etc/issue echo "" >> /etc/issue exit 0
$ sudo chmod a+x /etc/rc.local
The file will be executed as the root user during system boot
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:
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:
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"
Comment anonymously. Login not required.