How to Run a Script at Startup in Linux: A Step-by-Step Guide for Ubuntu, CentOS, and Debian

linux, shell bash, ubuntu

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.

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:

  1. 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
  1. 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
  1. 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.

See also  BlueStacks Linux (Android): A Step-by-Step Guide

CentOS: Using Crontab for Startup Scripts

CentOS, a favorite for server environments, provides a simple way to run startup scripts via crontab.

Steps:

  1. Create Your Script: Place your script in a suitable location, as described in the Ubuntu section.
  2. Edit the Crontab:
    Open the crontab editor:
   crontab -e
  1. 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:

  1. 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
  1. Register the Script:
    Use the update-rc.d command to register your script with the appropriate runlevels:
   sudo update-rc.d my_startup_script.sh defaults
  1. 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.
See also  How to disable password prompts (UAC) in Ubuntu

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.

Leave a Comment