What methods can be used to set an application to automatically start on system boot in Linux?
sobes.tech AI
Answer from AI
In Linux, there are several ways to set up an application to automatically start at system boot:
-
systemd service — a modern and recommended method. A unit file is created in
/etc/systemd/system/or~/.config/systemd/user/, describing how to start the application. The service is then enabled with the commandsystemctl enable service_name. -
init.d scripts — an older method for systems with SysVinit. Scripts are placed in
/etc/init.d/and added to the desired runlevels usingupdate-rc.dorchkconfig. -
crontab with @reboot — you can add a task to crontab with the directive
@rebootso that the command runs at startup. -
Autostart in user session — for graphical environments, you can add a script or shortcut to the autostart folder, for example
~/.config/autostart/.
Example of a systemd unit file:
[Unit]
Description=My Application
After=network.target
[Service]
ExecStart=/usr/local/bin/myapp
Restart=always
[Install]
WantedBy=multi-user.target
After creating the file, you need to execute:
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service