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

linux, shell bash, ubuntu

Running scripts automatically at startup can streamline tasks like launching applications, managing services, or performing system maintenance. This guide provides step-by-step instructions for setting up startup scripts on Ubuntu, CentOS, and Debian.

Why Run Scripts at Startup?

Automating scripts to execute during system startup can:

  • Start background processes without manual intervention.
  • Manage system maintenance tasks like backups or updates.
  • Ensure essential services are available immediately after boot.

Methods to Run Scripts at Startup

Different Linux distributions offer various methods to execute scripts at startup. Here’s how to set it up on Ubuntu, CentOS, and Debian.

Ubuntu: Using systemd

Ubuntu utilizes systemd for service management, making it a reliable choice for running startup scripts.

  1. Create Your Script:
    • Save your script in a directory like /usr/local/bin/.
    • Ensure it’s executable: sudo chmod +x /usr/local/bin/my_startup_script.sh
  2. Create a systemd Service File:
    • Open a new service file: 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
After=network.target

[Service]
ExecStart=/usr/local/bin/my_startup_script.sh
Type=simple
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
  1. Enable and Start the Service:
    • Reload systemd to recognize the new service: sudo systemctl daemon-reload
    • Enable the service to run at startup: sudo systemctl enable my_startup_service.service
    • Start the service immediately: sudo systemctl start my_startup_service.service

Your script will now execute automatically when Ubuntu starts.

CentOS: Using Cron’s @reboot

CentOS allows scheduling tasks at startup using cron’s @reboot directive.

  1. Create Your Script:
    • Place your script in a suitable location, such as /usr/local/bin/.
    • Ensure it’s executable: sudo chmod +x /usr/local/bin/my_startup_script.sh
  2. Edit the Crontab:
    • Open the crontab editor: crontab -e
    • Add the following line: @reboot /usr/local/bin/my_startup_script.sh
    • Save and exit the editor.
See also  How to install Ruby 1.8.7 on CentOS 5.5 Linux

The @reboot directive ensures your script runs each time the system starts.

Debian: Using rc.local

Debian systems can utilize the rc.local file to run scripts at startup.

  1. Create Your Script:
    • Save your script, for example, in /usr/local/bin/.
    • Ensure it’s executable: sudo chmod +x /usr/local/bin/my_startup_script.sh
  2. Edit rc.local:
    • Open the rc.local file: sudo nano /etc/rc.local
    • Add your script before the exit 0 line: /usr/local/bin/my_startup_script.sh exit 0
    • Save and exit the editor.
  3. Ensure rc.local is Executable:
    • Set the execute permission: sudo chmod +x /etc/rc.local

Your script will now run automatically at startup.

Tips for Running Startup Scripts

  • Test Before Rebooting: Execute your script manually to confirm it works as intended.
  • Use Logging: Redirect output to a log file for troubleshooting: /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.

FAQs

1. Can I run multiple scripts at startup?
Yes, you can configure multiple scripts to run at startup using any of the methods above. Ensure each script is properly configured and tested.

2. What if my script requires network access?
For scripts that depend on network availability, ensure they execute after the network is up. In systemd, include After=network.target in the [Unit] section.

3. How can I disable a startup script temporarily?
For systemd services, use:

sudo systemctl disable my_startup_service.service

For cron jobs, comment out the relevant line in the crontab file by adding # at the beginning.

4. Is it possible to delay the execution of a startup script?
Yes, you can add a delay within your script using the sleep command. For example, to delay for 60 seconds:

sleep 60

Leave a Comment