Sobes.tech
Junior — Middle

How can you set up automatic script execution on system startup in Linux?

sobes.tech AI

Answer from AI

In Linux, automatic script startup at system boot can be configured in several ways:

  1. systemd (modern method):

    • Create a unit file in /etc/systemd/system/, for example myscript.service.
    • Define which script to run in it.
    • Enable the service with the command systemctl enable myscript.service.

    Example of a unit file:

    [Unit]
    Description=My script at startup
    
    [Service]
    ExecStart=/path/to/your/script.sh
    Type=simple
    
    [Install]
    WantedBy=multi-user.target
    
  2. rc.local (if supported):

    • Add the script call to the /etc/rc.local file.
    • Make sure the file is executable (chmod +x /etc/rc.local).
  3. cron @reboot:

    • Add a line to crontab:
      @reboot /path/to/your/script.sh
      
  4. init.d scripts (for older systems):

    • Place the script in /etc/init.d/.
    • Make it executable.
    • Use update-rc.d or chkconfig to add it to autostart.

The choice of method depends on the distribution and version of the init system.