Run a Script on Startup in Linux

There are several ways to run a script on startup in Linux, depending on your specific distribution and the type of script you are trying to run. Each method has its own advantages and is suitable for different use cases.

Methods Overview

Method Best For Linux Systems User Level
systemd System services, daemons Modern distributions System-wide
cron (@reboot) Simple scripts, scheduled tasks All distributions User or system
rc.local Quick system scripts Traditional systems System-wide
init.d Legacy system services Older distributions System-wide
.bashrc/.bash_profile User-specific scripts All distributions Per user

Using systemd (Recommended)

On modern Linux systems, systemd is the preferred method for running scripts at startup. It provides better control, logging, and dependency management.

Creating a systemd Service

Create a service file in /etc/systemd/system/ directory:

sudo nano /etc/systemd/system/myscript.service

Add the following configuration:

[Unit]
Description=My startup script
After=network.target

[Service]
Type=simple
ExecStart=/path/to/your/script.sh
Restart=on-failure
User=root
WorkingDirectory=/path/to/script/directory

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable myscript.service
sudo systemctl start myscript.service

Using cron with @reboot

The cron method is simple and works across all Linux distributions. Use the @reboot directive to run scripts at startup.

Edit the crontab:

crontab -e

Add your script with the @reboot directive:

@reboot /path/to/your/script.sh

Verify the crontab entry:

crontab -l

Using rc.local

The rc.local file executes commands at the end of the boot process. This method is straightforward but less flexible than systemd.

Edit the rc.local file:

sudo nano /etc/rc.local

Add your script before the exit 0 line:

#!/bin/sh -e
# Your existing content...

# Run your script
/path/to/your/script.sh &

exit 0

Make rc.local executable:

sudo chmod +x /etc/rc.local

Using init.d (Legacy Systems)

For older Linux systems using SysV init, create scripts in the /etc/init.d directory.

Create the init script:

sudo nano /etc/init.d/myscript

Add the init script template:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          myscript
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: My startup script
### END INIT INFO

case "$1" in
  start)
    echo "Starting myscript..."
    /path/to/your/script.sh &
    ;;
  stop)
    echo "Stopping myscript..."
    # Add stop logic if needed
    ;;
  *)
    echo "Usage: /etc/init.d/myscript {start|stop}"
    exit 1
    ;;
esac

exit 0

Make executable and configure:

sudo chmod +x /etc/init.d/myscript
sudo update-rc.d myscript defaults

Key Points

  • Always use absolute paths for scripts and commands, not relative paths

  • Ensure your script has execute permissions: chmod +x script.sh

  • Test scripts manually before setting them to run at startup

  • Use & at the end of commands in rc.local to run scripts in background

  • Check system logs (/var/log/syslog) for troubleshooting startup issues

Verification

After configuring startup scripts, verify they work by:

# For systemd
sudo systemctl status myscript.service

# For cron
sudo grep CRON /var/log/syslog

# Check if process is running
ps aux | grep script_name

Conclusion

Linux offers multiple methods to run scripts at startup, with systemd being the modern standard for most distributions. Choose cron for simple user scripts, rc.local for quick system tasks, or init.d for legacy systems. Always use absolute paths and proper permissions for reliable startup script execution.

Updated on: 2026-03-17T09:01:38+05:30

86K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements