User Tools

Site Tools


ubuntu:systemd:enable_rc.local_with_systemd

Ubuntu - SystemD - Enable rc.local with SystemD

How to enable /etc/rc.local script to run on system startup.


Enable /etc/rc.local on SystemD

sudo systemctl status rc-local

returns:

○ rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; static)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: inactive (dead)
       Docs: man:systemd-rc-local-generator(8)

Try to enable /etc/rc.local to run on system boot

sudo systemctl enable rc-local

may return something like:

The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
 
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.

NOTE: The unit file has no [Install] section.

  • As such Systemd can not enable it.

The solution

Create a Service File

sudo vi /etc/systemd/system/rc-local.service

and populate with:

/etc/systemd/system/rc-local.service
[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local
 
[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99
 
[Install]
 WantedBy=multi-user.target

Create the /etc/rc.local file

printf '%s\n' '#!/bin/bash' 'exit 0' | sudo tee -a /etc/rc.local

NOTE: Modern versions of Ubuntu do not come with this /etc/rc.local file.

  • The file used to be provided in Ubuntu version 16.10 and earlier.

Make the /etc/rc.local file executable

sudo chmod +x /etc/rc.local

Enable the rc-local service on system boot

sudo systemctl enable rc-local

returns:

Created symlink from /etc/systemd/system/multi-user.target.wants/rc-local.service to /etc/systemd/system/rc-local.service.

Start the service and check its status

sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

returns:

● rc-local.service - /etc/rc.local Compatibility
 Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
 Active: active (running) since Fri 2023-06-02 09:40:46 BST; 5min ago
 Process: 921 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
 Main PID: 901 (watch)
 CGroup: /system.slice/rc-local.service

ubuntu/systemd/enable_rc.local_with_systemd.txt · Last modified: 2023/06/03 08:43 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki