Linux allows you to configure programs to start automatically during boot. The process varies based on the distribution and environment you are using. Whether you need a program to run at boot on a desktop or server environment, Linux offers several methods to achieve this.
Modern Linux systems typically use systemd to manage services during startup, while older versions may use System V init. Each method involves configuring specific files or scripts, depending on your system setup. Knowing which method to use depends on whether your environment loads a desktop interface like GNOME or KDE or operates in a console-only mode.
For desktop environments, configuring startup applications involves integrating with the session manager. On server systems, you may rely more on cron jobs or the rc.local script. Understanding these different methods is essential for ensuring that your programs run automatically as intended.
Methods to automatically run program on Linux startup:
Running a program automatically on Linux startup via systemd:
Modern Linux distributions primarily use systemd to manage services during boot. systemd handles various system processes and can be configured to autostart programs using service unit files. This method is recommended for most users because of its flexibility and widespread support.
To use systemd, you must ensure that the program you want to start has an associated service unit file. If it does not, you can create a custom one. The steps below will guide you through enabling a program to start automatically using systemd.
- 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 - Check if the service unit is enabled (optional).
$ sudo systemctl is-enabled mysql disabled
enabled service unit is executed during boot
- 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.
- Confirm if the service unit is enabled (optional).
$ sudo systemctl is-enabled mysql enabled
Running a program automatically on Linux startup via cron:
The cron daemon is a tool used to schedule tasks in Linux. You can configure it to run commands at specific times or during system startup. cron jobs are useful for running lightweight scripts or commands that do not require a full service management system like systemd.
Using cron to autostart a program is straightforward. You need to add a specific entry to the user’s crontab file. This method is particularly useful if you want to run a program as a specific user at boot.
- Open the 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.
- Insert a line starting with @reboot .
# m h dom mon dow command @reboot
@reboot defines the job to be executed during system boot.
- Add the command to start your program after the @reboot.
@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.
- Save the crontab file.
$ crontab -e crontab: installing new crontab $
The file is saved in /var/spool/crontab/<username>
- 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 traditionally used to execute commands during system startup. Although it is not used in modern systemd-based systems by default, it remains a valid method for running simple scripts at boot. It is often used in server environments where the simplicity of running commands at startup is desired.
To use rc.local, you must ensure that the script is executable and properly formatted. The script will run with root privileges, so it is critical to be cautious about the commands you add to it.
- 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
- 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)
- 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
- 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 a popular desktop environment for Linux, used by distributions like Ubuntu and Red Hat. Autostarting programs in GNOME requires adding them to the Startup Applications tool, which is designed for managing user-specific startup applications.
This method is simple and does not require editing configuration files. It is ideal for users who need certain applications to start automatically every time they log in to their GNOME session.
Running a program automatically on KDE startup:
KDE is another widely used desktop environment in Linux, known for its flexibility and customization options. Like GNOME, it has its own method for managing startup applications, accessible through the System Settings.
Autostarting a program in KDE is similar to GNOME but involves navigating through KDE’s settings. This method is user-friendly and doesn’t require advanced knowledge of Linux startup processes.
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"
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.