Is /etc/rc.local still the preferred way to run scripts at startup on Ubuntu 18.04 LTS

I used to run several scripts at startup to set things the way I want my server running. After migrating to Ubuntu 18.04, I am having trouble with a few scripts and I am just wondering if this is still the preferred method to run shell scripts automatically after rebooting?


Following Ubuntu 16.04 traditional init startup scripts have been superseded by the systemd service and its configurations. Most of the scripts or script instructions were rewritten into so called systemd unit files. Therefore I would recommend to setup a systemd service for your custom startup scripts.

Create /etc/systemd/system/foo.service with content:

[Unit]
Description=Setup foo
After=network.target

[Service]
Type=oneshot
ExecStart=/opt/foo/setup-foo.sh
RemainAfterExit=true
ExecStop=/opt/foo/teardown-foo.sh
StandardOutput=journal

[Install]
WantedBy=multi-user.target

Replace with your parameters accordingly. This service definition will run /opt/foo/setup-foo.sh on each startup.

Remember to load and enable the service:

sudo systemctl daemon-reload
sudo systemctl enable foo.service

For more info take a look at this example.