Running a script automatically at startup in Linux is a useful technique, whether you’re a system administrator managing multiple servers or a regular user looking to automate routine tasks. Automating tasks like system maintenance, launching applications, or running background services when your system boots can save you time and effort.
In this guide, I’ll walk you through how to run a script at startup on three of the most popular Linux distributions: Ubuntu, CentOS, and Debian. These instructions will help you ensure your scripts are triggered each time the system starts, making your Linux experience smoother and more efficient.
Table of Contents
Why Run a Script at Startup?
Automating startup scripts can handle tasks like:
- Starting background processes
- Managing logs or backups
- Monitoring system health
- Ensuring key services are available immediately after boot
By setting up these scripts to run on their own, you reduce manual intervention, especially on systems that require consistent uptime or availability.
Ubuntu: Running a Script at Startup with Systemd
Ubuntu, known for its user-friendly interface, uses systemd to manage services. This is one of the most reliable methods to ensure a script runs at startup.
Steps:
- Create Your Script: Save your script to a directory like
/usr/local/bin/
. For example:
sudo nano /usr/local/bin/my_startup_script.sh
Make sure it’s executable:
sudo chmod +x /usr/local/bin/my_startup_script.sh
- Create a systemd Service File: Open a new service file using your preferred text editor:
sudo nano /etc/systemd/system/my_startup_service.service
Add the following content, replacing the script path with your own:
[Unit]
Description=Run My Startup Script
[Service]
ExecStart=/usr/local/bin/my_startup_script.sh
[Install]
WantedBy=multi-user.target
- Enable and Start the Service:
After saving your service file, run the following commands to enable and start your service:
sudo systemctl enable my_startup_service.service
sudo systemctl start my_startup_service.service
Your script will now automatically run when Ubuntu starts up.
CentOS: Using Crontab for Startup Scripts
CentOS, a favorite for server environments, provides a simple way to run startup scripts via crontab.
Steps:
- Create Your Script: Place your script in a suitable location, as described in the Ubuntu section.
- Edit the Crontab:
Open the crontab editor:
crontab -e
- Schedule the Script to Run at Startup:
Add this line to your crontab file:
@reboot /usr/local/bin/my_startup_script.sh
Save and exit the crontab editor.
After rebooting, your script will automatically run. The @reboot
directive ensures it runs each time the system starts.
Debian: Running Scripts at Startup with SysVinit
Debian, one of the most respected and stable Linux distributions, uses the SysVinit system to handle startup scripts. This method is a bit more old-school but still widely used.
Steps:
- Create Your Script:
As with the previous methods, create your script and make it executable:
sudo nano /etc/init.d/my_startup_script.sh
sudo chmod +x /etc/init.d/my_startup_script.sh
- Register the Script:
Use theupdate-rc.d
command to register your script with the appropriate runlevels:
sudo update-rc.d my_startup_script.sh defaults
- Reboot to Test:
After registering, reboot your system:
sudo reboot
Your script should now run automatically at startup.
Tips for Running Scripts at Startup
- Test Before Rebooting: It’s a good idea to run your script manually to make sure it behaves as expected before setting it up to run at startup.
- Use Logging: Consider adding logging to your script so you can easily troubleshoot if something goes wrong. Redirect the output to a log file, for example:
/usr/local/bin/my_startup_script.sh >> /var/log/my_script.log 2>&1
- Check Permissions: Ensure your script has the necessary permissions and ownership, especially when accessing sensitive files or directories.
Final Thoughts
By learning how to run scripts at startup in Linux, you gain control over automating tasks that improve system functionality and save you valuable time. Whether you’re using Ubuntu’s systemd, CentOS’s crontab, or Debian’s SysVinit, the ability to automate processes at boot is a must-have skill for any serious Linux user.