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.
Table of Contents
Why Run Scripts at Startup on Linux?
Automating scripts to execute during system startup can:
- Run background processes automatically without manual intervention.
- Automate system maintenance tasks like backups, updates, and monitoring.
- Ensure critical services start on boot to maintain uptime and reliability.
Different Linux distributions provide various ways to execute startup scripts. Below, we cover the most effective methods for Ubuntu, CentOS, and Debian.
Run a Script at Startup on Ubuntu Using systemd
Ubuntu uses systemd, a modern service manager that provides a reliable way to run scripts at startup. This method is preferred because it offers fine-grained control over startup processes, ensuring your script runs after the necessary system services are available. In this guide, we’ll walk through creating a startup script, configuring it as a systemd service, and enabling it to run automatically when Ubuntu boots. Let’s begin…
Step 1: Create the Startup Script
First, save your script in a safe directory and make it executable.
# Navigate to the desired script directory
cd /usr/local/bin/
# Create the script file
sudo nano my_startup_script.sh
Add the following content inside the script file:
#!/bin/bash
echo "Script is running on startup" >> /var/log/startup_script.log
sudo chmod +x /usr/local/bin/my_startup_script.sh
Step 2: Create a systemd Service File
Systemd services need a configuration file in /etc/systemd/system/.
# Open a new service file
sudo nano /etc/systemd/system/my_startup_service.service
Add the following configuration:
[Unit]
Description=My Custom Startup Script # Description of the service
After=network.target # Ensures network is available before running the script
[Service]
ExecStart=/usr/local/bin/my_startup_script.sh # Path to the script
Type=simple # Runs the script as a simple process
RemainAfterExit=yes # Keeps the service active even after script execution
[Install]
WantedBy=multi-user.target # Ensures it starts in multi-user mode
Step 3: Enable and Start the systemd Service
Reload systemd so it recognizes the new service:
sudo systemctl daemon-reload
Enable the service to start on boot:
sudo systemctl enable my_startup_service.service
Start the service immediately:
sudo systemctl start my_startup_service.service
Check the status to confirm the service is running:
sudo systemctl status my_startup_service.service
Now, your script runs automatically every time Ubuntu boots. 🚀
Run a Script at Boot on CentOS Using Cron @reboot
CentOS provides multiple ways to schedule tasks on startup, but one of the simplest and most efficient methods is using cron jobs with the @reboot directive. This method allows you to run a script automatically on system boot without needing to create a dedicated service. We’ll show you how to create a startup script, schedule it with crontab, and ensure it runs every time CentOS reboots.
Step 1: Create the Script
First, create the script in a safe directory:
# Navigate to the script directory
cd /usr/local/bin/
# Create the script file
sudo nano my_startup_script.sh
Add the script content:
#!/bin/bash
echo "CentOS startup script executed" >> /var/log/startup_script.log
Make the script executable:
sudo chmod +x /usr/local/bin/my_startup_script.sh
Step 2: Add the Script to Cron Jobs
Edit the cron table for the current user:
crontab -e
Add this line at the end:
@reboot /usr/local/bin/my_startup_script.sh
Save and exit.
To verify if the job is scheduled, run:
crontab -l
Now, your script executes automatically on CentOS startup. 🎯
Run a Script at Startup on Debian Using rc.local
Debian-based systems traditionally use the rc.local file to execute scripts at boot. While this method is considered legacy, it remains a quick and effective way to automate startup tasks. In this section, we’ll guide you through creating a startup script, adding it to rc.local, and ensuring it runs automatically when Debian starts.
Step 1: Create Your Script
Create the script file:
sudo nano /usr/local/bin/my_startup_script.sh
Add this content:
#!/bin/bash
echo "Debian startup script executed" >> /var/log/startup_script.log
Make it executable:
sudo chmod +x /usr/local/bin/my_startup_script.sh
Step 2: Edit the rc.local File
Open the rc.local file:
sudo nano /etc/rc.local
Add the script execution command before exit 0:
/usr/local/bin/my_startup_script.sh
exit 0 # Ensures the script runs before the system exits the boot process
Save and exit.
Step 3: Ensure rc.local is Executable
Set executable permissions:
sudo chmod +x /etc/rc.local
All done, now you can automate scripts at start-up on Debian Linux.
Tips for Running Linux Startup Scripts Successfully
- 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
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.
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.
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.
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