Auto run after booting up OS

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

  1. Open the /etc/rc.local file with root privileges using a text editor like nano:
sudo nano /etc/rc.local
  1. 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
  1. Save and close the file.

  2. Make sure /etc/rc.local has execute permissions:

sudo chmod +x /etc/rc.local
  1. Reboot your system to test the script execution.

Method 2: Using systemd service

  1. Create a new systemd service file:
sudo nano /etc/systemd/system/myscript.service
  1. 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
  1. Save and close the file.

  2. Enable and start the service:

sudo systemctl enable myscript.service
sudo systemctl start myscript.service
  1. Check the status of the service:
sudo systemctl status myscript.service
  1. Reboot your system to test the script execution. The script will be executed automatically after booting up.
posted @ 2024-07-15 09:41  Fredqq  阅读(77)  评论(0)    收藏  举报