Introduction
Running scripts at system boot is a common requirement for many Linux users and administrators. This guide will walk you through the process of setting up Linux scripts to run automatically when your system starts up.
- Method 1: Using crontab
- Method 2: Using systemd services
Method 1: Using crontab
One of the simplest ways to run a script at boot is by using crontab. The cron daemon is a Linux utility that runs commands at specific times or intervals.
To edit the crontab file, run:
crontab -e
Add the following line to run your script at boot:
@reboot /path/to/your/script.sh
Method 2: Using systemd services
On systems using systemd (most modern Linux distributions), you can create a service to run your script at boot.
Create a new file in /etc/systemd/system/, for example:
sudo nano /etc/systemd/system/my_script.service
Fill it with:
[Unit]
Description=My Script
After=network.target
[Service]
ExecStart=/path/to/your/script.sh
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
Reload systemd daemon and enable the service:
sudo systemctl daemon-reload
sudo systemctl enable my_script.service