How To Run A Command On StartUp in Linux (works on Centos & Ubuntu)
Enjoying this content? Subscribe to the Channel!
Automate Your Life! How to Run Any Command on Startup in Linux (CentOS, Ubuntu, & Raspberry Pi)
Welcome back to Darren’s Tech Tutorials!
One of the most powerful things you can do in Linux is automate repetitive tasks. If you have a specific script, service start command, or utility you need running every single time your system boots up, knowing how to trigger it automatically is essential.
In this comprehensive guide, we are going to show you the reliable method for executing commands during the Linux startup process using the rc.local file. This classic method is incredibly versatile and works across major distributions, including CentOS 7, Ubuntu, and even the Raspberry Pi!
Quick Tip: Before we dive in, don’t forget to grab your free, handy Linux Cheat Sheet! http://eepurl.com/dkRNM9
Understanding rc.local: Your Startup Command Gateway
Historically, the /etc/rc.local file was the primary location for system administrators to add personalized commands that needed to execute right at the end of the standard boot process. While newer systems often rely on systemd services, the rc.local mechanism is still supported and often the quickest way to get a simple command running at startup without defining a complex service file.
We will be using CentOS 7 as our example, but the steps are virtually identical for Ubuntu and Raspberry Pi (Raspbian).
Step-by-Step: Adding Your Command to Startup
The entire process involves three main actions: locating and editing the file, inserting your desired commands, and ensuring the file has the correct permissions to run.
Step 1: Locating and Editing the rc.local File
The /etc/rc.local file may or may not exist in modern distributions. If it doesn’t exist, we will create it. We recommend using a powerful command line editor like vi or a simpler editor like nano.
Use the following command to open the file for editing:
sudo vi /etc/rc.local
Step 2: Inserting Your Startup Commands
Once the file is open, you need to add the commands you want to execute on boot.
Important Note: Every command you run here must be fully defined with its absolute path if it’s a script or a program not located in your standard $PATH environment.
As an example, let’s add a command that simply logs the date and time every time the system starts up:
#!/bin/bash
# Log the system boot time
echo "System successfully booted on: $(date)" >> /var/log/boot-log.txt
# --- YOUR CUSTOM COMMANDS GO HERE ---
# Example: Start a simple Python script in the background
# /usr/bin/python3 /opt/myscript/start_service.py &
exit 0
Key Requirement: Make sure that the exit 0 line remains the very last line of the file. Any commands you add must appear before this line. Once you have added your desired content, save and close the editor.
Step 3: Granting Execute Permissions
This is the most crucial step, especially on CentOS and Ubuntu. Even if the rc.local file exists, if it does not have execute permission (+x), the system will ignore it during startup.
Use the chmod command to make the file executable:
sudo chmod +x /etc/rc.local
This command ensures that the system is allowed to run the commands contained within the file.
Step 4: Reboot and Test
Your startup configuration is now complete! The only way to verify that your command is running successfully is to reboot the system.
Execute the reboot command:
sudo reboot
After your system comes back online, check to see if your command successfully executed.
For our example: We created a simple log file at /var/log/boot-log.txt. After rebooting, you can check its content:
cat /var/log/boot-log.txt
If you see the logged date and time, congratulations—you have successfully run a command on startup!
For a complete list of commands used in this tutorial, please visit the accompanying article: https://darrenoneill.eu/?p=648
Wrapping Up and Taking Control
You have now mastered one of the fundamental techniques for system automation in Linux! Whether you are deploying a dedicated server, setting up a Raspberry Pi for a home automation project, or just ensuring a utility starts correctly, the rc.local method provides immediate and robust command execution upon boot.
Give this technique a try with your own scripts and start automating your Linux experience today.
If you had any difficulty following these steps or have questions about how to adapt this to a specific distribution, please drop a comment below—we’re here to help!
Don’t forget to like this post and subscribe to Darren’s Tech Tutorials for more clear, accessible technology guides! Happy automating!