How to run programs/scripts at the startup with Linux

How to Run Programs & Scripts on Startup in GNU/Linux

Once you use Linux for a while, the number of applications you use and the number of manual tasks you perform each time will grow. Starting a program once you boot the OS could be one of these annoying manual tasks that we often don’t like to do every time. Here is how to do this easily.

To start a script or program on the system, it must be configured through the GUI, adding a service through systemd, introducing crontab rules or by adding the commands to the bash_profile. The best method will depend on the requirements and your needs.

In this article, I will explain the different ways to start a program or script on startup, the benefits of each method and the situations in which you can use each one.

Want the best experience? Become a premium member for ad-free browsing, access exclusive content, and ask questions in our private forums. Your membership helps support the site!
Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

Method 1: Using the GUI (Easiest method)

In Linux, most distributions include a built-in option to configure the automatic startup of programs or scripts. These options may vary depending on your desktop environment, but they usually share a similar name.

Theory

In my case, I will use Xubuntu (Ubuntu + XFCE) to show the example. When opening the menu window, and looking for “startup” the “Session and Startup” option is the one we are looking for to make this configuration:

Once you open the window, you will have to choose the option “Application AutoStart”:

You will surely notice several programs that start automatically on the system by default. You can enable/disable auto-start on the system by unchecking the boxes, but the option we are most interested in is the create option, which is found with the + button at the bottom:

In the window of creation of the application, you will notice this window which contains these fields that you can fill in:

  • Name: Contains the descriptive name of the application, it can be anything.
  • Description: A description is used to detail any relevant program information so it can be easily identified.
  • Command: Here will go the command or script you are going to use. If you are using a script, you must indicate the directory where it is located. In some cases, you must identify the binary and its directory of the language you are going to use (bash or python, for example).

Note: When using programs, you must identify if the command to be used exists (by running it first in the terminal). In case it does not exist in the path environment, you must identify the directory of the program and place it in the command.

Example

Now, let’s consider the following script:

#!/bin/bash
echo "Script ran at: $(date)" >> /home/sysadmin/Desktop/startup_log.txt

It is a simple script that will write in a text file, a log that indicates the time and date each time it is executed:

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

I placed this script on my system desktop (/home/sysadmin/Desktop/scripts/logtime.sh) and it will write the date and time to the text file indicated in the script. Now if we implement it using the GUI, it would look like this:

Let’s take a look only at the Command field. Since we chose to use a script in bash, we can directly specify the bash directory and then the script directory. But we can also put the script directory and the system will still run the script, but it does not always work.

We can run an application/program directly using only the command if it can be used in the terminal. If not, you can specify the directory-path of the binary of the program, in both cases, it will work correctly.

Bonus: Find the full path of an application

If you want to know the directory of an application/program you can use the following command:
which <command>

The terminal must have the ENV PATH constant to run it. It’s like having a shortcut of the binary linked to the command. But in some cases, this path could change. In those cases, it is better to use the directory-path of the binary.

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.

Method 2: Service file using systemd (Most recommended)

Systemd is the most recommended method in systems where you do not have a GUI, since it will allow you to configure “services” that will act as daemons, running in the background, and in turn, you can configure them to start at system startup.

Theory

Although the configuration of a service in Systemd looks complex, it is quite useful, since it can allow you to specify which user you want to use the service, if you want the service to run before or after some other specific service and many other things.

At first, the most basic form of the systemd structure is this:

[Unit]
Description=My Example Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/example-binary
Restart=on-failure
User=example-user
Group=example-group

[Install]
WantedBy=multi-user.target
  • Description: This provides a brief explanation of what the service does.
  • After: This specifies the order in which services are started. This indicated that the service will start AFTER some other service.
  • Type: Defines the process start-up type. simple is the default and indicates that the service starts the specified ExecStart command and it will continue running. Other types include forking, oneshot, dbus, notify, and idle.
  • ExecStart: This specifies the command to be executed to start the service. It must be an absolute path. This is the main command that runs the service.
  • Restart: Configures whether and when systemd automatically restarts the service if it exits.
  • WantedBy: This specifies the target under which this service should be started. multi-user.target is a standard system target for setting up a multi-user, non-graphical environment. This is especially important to enable the service to run at the startup.

Script example

Now let’s go back to the example script that I made in the first section, the first thing you should do is create a service file inside the directory /etc/systemd/system you can use Nano like this:
sudo nano /etc/systemd/system/log-date.service

Note: Remember to use sudo, because the directory is part of the system and root is required to write data or create files there.

If we want to add the script to the service file, it would look like this:

[Unit]
Description=Run logtime.sh script at startup using bash
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash /home/sysadmin/Desktop/scripts/logtime.sh
Restart=on-failure
User=sysadmin
Group=sysadmin

[Install]
WantedBy=multi-user.target

The first thing we notice is that we specify the bash directory (it is necessary and common practice in this method) and then we specify the script directory (it’s like telling the service file this: “Hey, use this program to run this script!”).

Program example

Now, if you only want to run a program, you can specify the path-directory of the binary. Again, we can use the which command to know where is located the binary of any command, and then fill the service file like that:

[Unit]
Description=Run firefox at startup
After=network.target

[Service]
Type=simple
ExecStart=/snap/bin/firefox
User=sysadmin
Group=sysadmin

[Install]
WantedBy=multi-user.target

Enabling and managing the service

After creating the service in either case, we can run it in the background with the following command:
sudo systemctl start log-date.service
sudo systemctl start firefox.service

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

You can also check the status of the service and verify if it’s working correctly using:
sudo systemctl status log-date.service
sudo systemctl status firefox.service

Now, let’s focus on what we are interested in, to make the service start at the system startup, you can do it using:
sudo systemctl enable log-date.service
sudo systemctl enable firefox.service

Finally, if you just want to stop the service you can use:
sudo systemctl stop log-date.service
sudo systemctl stop firefox.service

And for disabling the service, to avoid starting it at the OS startup:
sudo systemctl disable log-date.service
sudo systemctl disable firefox.service

Method 3: Using crontab

Cron is a tool that allows us to schedule tasks, which can be executed at a specific time (through date or time) or at different specific intervals. In our case, we can schedule it to run only at the system startup.

Unlike systemd, the process of creating a cronjob list is much more simplified and easier, but it also offers fewer features than the previous one has. So you should consider if you need a different feature or if you want something simple and straightforward.

To add a cronjob rule, you will have to modify the cronjob file using this command:
crontab -e

Now, all you have to do is add the command and the script to the last available line of the file. You will also need to add the argument @restart in that way we can say to the OS to start the instruction after booting.

We can also specify programs, adding the path-directory of the binary like you did in the previous sections. Like this:

Also, if you want to remove a rule, you can simply delete it and it will no longer run.

Related: Ubuntu Backup Essentials: Tips and Tricks from a sysadmin

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Method 4: Adding commands to .bashrc or .bash_profile

bashrc and bash_profile are bash utility scripts that execute different tasks and functions together with the system. Inside the files of each script, you can add instructions that will be executed depending on the situation.

Since there are two files, there are two different ways to use them in each situation:

  • .bashrc handles the shell environment for non-login interactive shells and is run when a terminal or session is started within a graphical environment.
    This is usually used to set environment variables, define aliases and create shell functions.
  • .bash_profile is focused on login shells, which are initiated during system login via SSH, console login, or remote access.
    Its primary role is to set up the environment for the user’s session by running initialization scripts and configuring session-wide settings.

Let’s take the case of .bash_profiles, which takes into effect once a shell login is started. It’s a different approach from the previous methods because this process will start every time you login to the shell and not in the system-startup, and also it can be specified to only one user.

To do this, it is necessary to modify the .bash_profile file, which is located in the home directory, there are cases where the file does not exist in the directory, so you will have to create it using any text editor on those moments. Keep in mind that every configuration is linked to only that user.

You can verify if the files exist using ls, and also adding an extra argument for showing all the hidden files (because this file is usually hidden):
ls -a

In case it does or does not exist, you can create or modify it using:
sudo nano .bash_profile

Inside the file, you will need to add this code:

.bash_profile
# ~/.bash_profile: executed by bash for login shells.

# Source the .bashrc if it exists
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# Run the logtime.sh script at login
/bin/bash /home/sysadmin/Desktop/scripts/logtime.sh

The first fragment of the code is a recommended part to add if it does not exist in the file, since it allows you to add all the existing characteristics in the .bashrc file (environment variables, aliases or functions).

Now the last section, takes care of what we are interested in: the script or program. As we are using bash_profile, I will not add the firefox bin (because we don’t have GUI in this case!), but the process is identical.

And just like the previous section, if you want the bash_profile to prevent starting any process or program on the OS startup, you can simply delete the related lines in the file.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Similar Posts