5

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?

5
  • 2
    askubuntu.com/a/719157/158442 – muru Jun 19 '18 at 4:14
  • 1
    It depends. Best to properly configure your server so that you don't need any scripts. Second choice is to write a proper systemd script. We really can't advise you unless you post your scripts – Panther Jun 19 '18 at 6:28
  • I would make an entry in a runlevel. It is the recommended way. – abu_bua Jun 30 '18 at 13:06
  • @abu_bua "n recent versions of Linux systems such as RHEL 7, Ubuntu 16.04 LTS, the concept of runlevels has been replaced with systemd targets." (ostechnix.com/check-runlevel-linux) – jarno Aug 27 '19 at 7:37
  • @dsstorefile1 so what is the recommended way? – jarno Aug 27 '19 at 7:59
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.