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:
-
systemd (modern method):
- Create a unit file in
/etc/systemd/system/, for examplemyscript.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 - Create a unit file in
-
rc.local (if supported):
- Add the script call to the
/etc/rc.localfile. - Make sure the file is executable (
chmod +x /etc/rc.local).
- Add the script call to the
-
cron @reboot:
- Add a line to crontab:
@reboot /path/to/your/script.sh
- Add a line to crontab:
-
init.d scripts (for older systems):
- Place the script in
/etc/init.d/. - Make it executable.
- Use
update-rc.dorchkconfigto add it to autostart.
- Place the script in
The choice of method depends on the distribution and version of the init system.