To execute a script automatically after booting up to Ubuntu OS, you can place the script in the /etc/rc.local
file or create a systemd service. Here's how to do it using both methods:
Method 1: Using /etc/rc.local
- Open the
/etc/rc.local
file with root privileges using a text editor like nano:
sudo nano /etc/rc.local
- Add your script at the end of the file, ensuring that it has execute permissions (if not already set). For example, if your script is located at
/home/user/myscript.sh
, add the following line:
/bin/bash /home/user/myscript.sh
-
Save and close the file.
-
Make sure
/etc/rc.local
has execute permissions:
sudo chmod +x /etc/rc.local
- Reboot your system to test the script execution.
Method 2: Using systemd service
- Create a new systemd service file:
sudo nano /etc/systemd/system/myscript.service
- Add the following content to the file, replacing
/path/to/your/script.sh
with the actual path to your script:
[Unit]
Description=My Script Service
After=network.target
[Service]
ExecStart=/bin/bash /path/to/your/script.sh
Restart=on-failure
User=root
Group=root
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myscript
[Install]
WantedBy=multi-user.target
-
Save and close the file.
-
Enable and start the service:
sudo systemctl enable myscript.service
sudo systemctl start myscript.service
- Check the status of the service:
sudo systemctl status myscript.service
- Reboot your system to test the script execution. The script will be executed automatically after booting up.