Categories
SysOps

How to execute rc.local shell script during boot using systemd

Execute rc.local shell script during boot using systemd compatibility generator.

Create rc.local shell script.

$ cat /etc/rc.local
#!/bin/bash
# do something
touch /tmp/boot
exit 0

Ensure that the executable bit is set.

$ sudo chmod +x /etc/rc.local

Execute systemd-rc-local-generator to create a systemd service.

Increase the log level to see what is going on.
$ sudo SYSTEMD_LOG_LEVEL=debug /usr/lib/systemd/system-generators/systemd-rc-local-generator
Automatically adding rc-local.service.

Inspect service status.

$ sudo systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: active (exited) since Sat 2020-07-04 22:43:40 UTC; 44s ago
       Docs: man:systemd-rc-local-generator(8)
    Process: 201 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
Jul 04 22:43:39 kube-master systemd[1]: Starting /etc/rc.local Compatibility...
Jul 04 22:43:40 kube-master systemd[1]: Started /etc/rc.local Compatibility.

Inspect service configuration.

$ cat /lib/systemd/system/rc-local.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

Common issues

$ sudo SYSTEMD_LOG_LEVEL=debug /usr/lib/systemd/system-generators/systemd-rc-local-generator
/etc/rc.local does not exist, skipping.

Create rc.local shell script.

$ sudo SYSTEMD_LOG_LEVEL=debug /usr/lib/systemd/system-generators/systemd-rc-local-generator
/etc/rc.local is not marked executable, skipping.

Set executable bit.