Since 2016 · 50,000+ customers · ★ 5.0 on Google Live chatWhatsAppTelegramOpen a ticket
Get started
Linux

How to Run a Command or Script at Startup in Linux

How to run a command at startup in Linux guide by 99RDP

There are several ways to run something automatically when Linux boots: a systemd service, a cron @reboot job, the old rc.local file, or desktop autostart for graphical apps. The right one depends on whether the command needs root, the network, or a logged-in desktop. This guide shows each method with working examples for Ubuntu, Debian, RHEL-based systems and others.

Quick answerFor anything important, create a systemd service:

sudo tee /etc/systemd/system/myscript.service >/dev/null <<'EOF'
[Unit]
Description=Run my script at boot
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/myscript.sh

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable myscript.service
A systemd oneshot unit file that runs a script at boot, open in nano on Ubuntu
A systemd service that runs at boot

Which method to use

MethodRuns asBest for
systemd serviceroot or any userScripts and long-running programs; ordering, logging, restarts
cron @rebootThe crontab's userQuick, simple tasks
/etc/rc.localrootLegacy setups (needs enabling on modern systems)
Desktop autostartThe logged-in userGraphical apps after login
Guide to choosing how to run a command at startup: systemd, cron @reboot, rc.local, desktop autostart
Pick a method

Create the script and make it executable:

sudo nano /usr/local/bin/myscript.sh
#!/bin/bash
echo "Booted at $(date)" >> /var/log/myscript.log

sudo chmod +x /usr/local/bin/myscript.sh

Create the unit file shown in the quick answer, then enable it. Use:

  • Type=oneshot for a script that runs and exits.
  • Type=simple with Restart=on-failure for a program that keeps running (a bot, a worker).
  • User=youruser in [Service] to run as a normal user instead of root.
  • After=network-online.target when the command needs the network.

Test it without rebooting, and read its output:

sudo systemctl start myscript.service
systemctl status myscript.service
journalctl -u myscript.service

Method 2: cron @reboot

crontab -e          # or: sudo crontab -e for root
@reboot /usr/local/bin/myscript.sh >> /tmp/myscript.log 2>&1
@reboot sleep 30 && /usr/local/bin/needs-network.sh

cron starts early in the boot, and the network may not be ready yet; a short sleep helps, but a systemd service with After=network-online.target is more reliable. Use full paths, since cron has a minimal PATH.

crontab -l listing two @reboot lines that run scripts at startup
A cron @reboot job

Method 3: rc.local

Many older guides use /etc/rc.local. On modern Ubuntu and Debian it doesn't exist by default, but systemd still runs it if you create it and make it executable:

sudo tee /etc/rc.local >/dev/null <<'EOF'
#!/bin/bash
/usr/local/bin/myscript.sh
exit 0
EOF
sudo chmod +x /etc/rc.local
sudo systemctl start rc-local     # runs it now; it will also run at boot

On RHEL-based systems the file is /etc/rc.d/rc.local and also needs chmod +x. For new setups, prefer Method 1.

Method 4: Run a program when you log in to the desktop

For graphical apps, use the desktop's autostart settings (GNOME Tweaks → Startup Applications, or KDE System Settings → Autostart), or create a file in ~/.config/autostart/:

[Desktop Entry]
Type=Application
Name=My App
Exec=/usr/bin/myapp --minimized

Method 5: A per-user systemd service

To run something as your user without root, put a unit in ~/.config/systemd/user/ and enable it with systemctl --user enable --now name.service. Run sudo loginctl enable-linger $USER so it starts at boot even before you log in.

Frequently asked questions

How do I run a command at startup in Linux?

Create a systemd service with your command in ExecStart and enable it, or add an @reboot line to your crontab.

How do I run a script at boot on Debian or Ubuntu?

Use a systemd service. /etc/rc.local also works if you create it, make it executable and start rc-local.

Why doesn't my @reboot cron job work?

Usually the network or a mount isn't ready yet, or the script relies on a PATH that cron doesn't have. Add a delay, use full paths, or switch to systemd.

How do I run a script at startup without root?

Use your own crontab (crontab -e) with @reboot, or a user systemd service with lingering enabled.

Nishant Kumar, Sr. Server Engineer at 99RDP
Written byNishant KumarSr. Server Engineer, 99RDP. Part of the team that runs our servers and answers support.
Keep reading

More Linux guides

All Linux guides

Need a server for this?

✓ Setup in minutes✓ Full admin access✓ 7-day refund on new services