If you’re interested in the service approach, maybe I can guide you a little bit. Might be handy.
So, if it’s a root specific script, meaning you want it to be executed by root, then I create a folder under /root for my scripts. Of course, you can basically have it anywhere you want but it’s the way I do things so, in case you want your script to be somewhere else, then adjust:
sudo mkdir /root/.scripts
The above is just a directory I create for all these scripts I want to run under root and so I remember where they are. Again, you can have them where you like really. Just adjust the path accordingly.
Note that you should replace <> with whatever you want for your specific use case.
Create your script file:
sudo nano /root/.scripts/<yourscriptname>.sh
Then add the content of your script inside, save and exit. Use what editor you want of course, it’s just for simplicity.
sudo chmod +x /root/.scripts/<yourscriptname>.sh
Then we create the unit file:
sudo nano /etc/systemd/system/<yourscriptname>.service
Add something like this:
[Unit]
Description=<Write a description of what the script is doing.>
After=multi-user.target
[Service]
ExecStart=/root/.scripts/<yourscriptname>.sh
Type=simple
[Install]
WantedBy=multi-user.target
Alias=<InCaseYouWantADifferentNameUseAnotherHere>.service
“Description” and “Alias” is not really needed but can be handy. If you don’t need or want, just remove the whole line.
Now:
sudo chmod 664 /etc/systemd/system/<yourscriptname>.service
Enable the service:
sudo systemctl enable <yourscriptname>.service
Reboot.
To verify if it got executed after boot, you can do this:
journalctl -b | grep <yourscriptname>
Hope that helps.