Ubuntu is the most popular Linux OS distribution. Using Ubuntu you have the opportunity to customize the system to suit your needs. You can customize many important processes using this OS. One such feature is setting up script autoloading, which allows you to automatically perform certain tasks when the system starts. This is especially useful if you have scripts or services that you want to run automatically on every boot.
This article will provide a guide on how you can enable script autoloading in Ubuntu using various methods.There are many ways, but in this article we will look at 3 of the most popular ones.
Installing the script using the /etc/init.d/ directory
Ubuntu has special shell scripts that respond to the start , stop , restart , and (supported by) reload commands to control a specific service. This is using the /etc/init.d/ directory. Here you can save your scripts for system initialization and comfortable use of Ubuntu. You can create your own script and add it to this directory. For example, let’s say you have a script called examplescript that you want to run automatically when the system boots. You can copy this script to the /etc/init.d/ directory and then use the following commands:
sudo chmod +x /etc/init.d/examplescript
sudo update-rc.d examplescript defaults
After successfully updating these two commands, your script will run successfully every time you log in.
Installing the script using the /etc/rc.local directory
You can also edit the /etc/rc.local file and add a call to your script before the exit 0 line. For example:
Today, Linux distributions, including Ubuntu, have newer systems, including Systemd. They replaced the rc.local script, but it can be restored even though it is the recommended solution. Next we’ll look at using systemd to run an automatic script.
Using systemd to enable script autoloading in Ubuntu
Ubuntu OS is very popular, hence the frequent releases and support from the developers of this distribution. Starting from version 15.04, the systemd initialization system is used. You can create your own unit file for your script. Create a file with the extension .service in the /lib/systemd/system/ directory. For example, examplescript.service, and add the following content:
Description=My Testing script1
One of the main tasks that the systemd utility does well is intensive parallelization of service startup during the system boot process. Thanks to this feature, you have the opportunity to launch the operating system faster.
After successful creating the file, run the following commands:
sudo systemctl daemon-reload
sudo systemctl enable examplescript.service
sudo systemctl start examplescript.service