Skip to content

Enable rc.local with Systemd

You might have been used to run commands after booting your machine, just by putting whateer you needed into /etc/rc.local. Most distros now use systemd, and there is no support (usually) to have that file ran at boot.

Enable /etc/rc.local

Run the following command at a terminal :

sudo systemctl status rc-local

You might get this error :

● rc-local.service - /etc/rc.local Compatibility
 Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
 Active: failed (Result: exit-code) since Thu 2015-11-26 23:54:58 CST; 59s ago
 Process: 1001 ExecStart=/etc/rc.local start (code=exited, status=1/FAILURE)
Nov 26 23:54:57 vivid rc.local[1001]: File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 920, in require
Nov 26 23:54:57 vivid rc.local[1001]: needed = self.resolve(parse_requirements(requirements))
Nov 26 23:54:57 vivid rc.local[1001]: File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 807, in resolve
Nov 26 23:54:57 vivid rc.local[1001]: raise DistributionNotFound(req)
Nov 26 23:54:57 vivid rc.local[1001]: pkg_resources.DistributionNotFound: shadowsocks==2.8.2
Nov 26 23:54:58 vivid sudo[1008]: pam_unix(sudo:session): session closed for user root
Nov 26 23:54:58 vivid systemd[1]: rc-local.service: control process exited, code=exited status=1
Nov 26 23:54:58 vivid systemd[1]: Failed to start /etc/rc.local Compatibility.
Nov 26 23:54:58 vivid systemd[1]: Unit rc-local.service entered failed state.
Nov 26 23:54:58 vivid systemd[1]: rc-local.service failed.

Or, if you try to enable it :

sudo systemctl enable rc-local

You may get:

The unit files have no [Install] section. They are not meant to be enabled
 using systemctl.
 Possible reasons for having this kind of units are:
 1) A unit may be statically enabled by being symlinked from another unit's
 .wants/ or .requires/ directory.
 2) A unit's purpose may be to act as a helper for some other unit which has
 a requirement dependency on it.
 3) A unit may be started when needed via activation (socket, path, timer,
 D-Bus, udev, scripted systemctl call, ...).

How to make it work ?

The error is pretty self explanatory/ The “unit file”, which is a file describing the service, has no [Install] section. As such Systemd can not enable it.
We can edit the file, and put our own description.

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

And just paste the following :

[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

Save/Close , and then update the file , in case it doesn’t exists :

if [ ! -f /etc/rc.local ]; then sudo echo -e "#!/bin/bash\n\nexit 0" > /etc/rc.local ; fi

And set the executable flag for the file :

sudo chmod +x /etc/rc.local

After that, enable the service on system boot:

sudo systemctl enable rc-local

Output:

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

Now start the service and check its status:

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

Output:

● rc-local.service - /etc/rc.local Compatibility
 Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
 Active: active (running) since Fri 2015-11-27 00:32:56 CST; 14min ago
 Process: 879 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
 Main PID: 880 (watch)
 CGroup: /system.slice/rc-local.service

Another way : Using Cron @reboot

If you don’t like the solution, if you don’t want to mess with services, or just want a simpler solution to execute something at boot, you can use the @reboot feature of cron. For example, if i want to restart httpd right after boot, it can be done like this :

sudo crontab -e

And add this line at the end :

@reboot systemd restart httpd

Save and close the file. (and reboot to test)

You have to check cron is enabled. Some distros have it disabled by default, and some distros use a different name for the service. Usually you can find it with the name “crond” “cron” “crontab” or “cronie”

sudo systemctl enable crond

That’s it.

Published inLinux

Comments are closed.

Ivn Systems/Software ©2020