Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hardware and software Environment

  • Ubuntu 18.04 64 – bit
  • Anaconda with python 3.6

Problem description

A startup command written to the /etc/rc.local file is not executed on a later version of Ubuntu. After some research, it turns out that Ubuntu has been using systemd instead of initd since version 16.04

To analyze problems

Run the systemctl command to check the status of rC-local

sudo systemctl status rc-local
Copy the code

Output error message

Rc-local. service - /etc/rc.local Compatibility Loaded: Loaded (/lib/systemd/system/rc-local.service; enabled-runtime; └─debian.conf Active: failed (Result: preset: enabled) Drop In: /lib/systemd/system/ rC-local.service. d ├ ─debian.conf Active: failed (Result: preset: enabled) exit-code) since Thu 2018-11-01 10:56:36 CST; 1h 59min ago Docs: man:systemd-rc-local-generator(8) Process: ExecStart=/etc/rc.local start (code=exited, status=203/EXEC) 11月 01 10:56:36 Ubuntu Systemd [1]: Starting /etc/rc.local Compatibility... Ubuntu Systemd [1961]: RC-local. service: Failed to execute command: Exec Format Error 11月 01 10:56:36 Ubuntu Systemd [1961]: Rc-local. service: Failed at step Exec spawning /etc/rc.local: Exec Format E 11月 01 10:56:36 Ubuntu Systemd [1]: rC-local. service: Control process exited, code=exited status=203 11月 01 10:56:36 Ubuntu Systemd [1]: rc-local.service: Failed with result 'exit-code'. 11月 01 10:56:36 Ubuntu Systemd [1]: Failed to start /etc/rc.local CompatibilityCopy the code

Enable the RC-local service in Systemd

sudo systemctl enable rc-local
Copy the code

Output error message

xugaoxiang@ubuntu:~$ sudo systemctl enable rc-local 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: 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, ...) . 4) In case of template units, the unit is meant to be enabled with some instance name specified.Copy the code

The rC-local service file does not contain Install fields such as WantedBy, RequiredBy, Also, and Alias. If not, the system does not consider it a Systemd service.

To fix the problem

The default service file is in the /etc/systemd/system directory, a bit like a service configuration file. Note that /lib/systemd/system also has a rc-local.service. We borrowed this template to modify it, or you can write it from scratch

sudo cp /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
Copy the code

The following changes are made: Add the Install field

# 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 [Install] WantedBy=multi-user.targetCopy the code

The Unit field describes the startup sequence and dependencies of the Service, the Service field describes how to start the Service, and the Install field describes how to Install the Service. Ubuntu 18.04 removes the /etc/rc.local file by default, so you need to manually create one and write the commands you want to run on startup into the file, as shown in the following example

#! /bin/bash /usr/bin/sslocal -c /home/xugaoxiang/Tools/ss/ss.json &Copy the code

Also, don’t forget to add executable permissions to /etc/rc.local

sudo chmod a+x /etc/rc.local
Copy the code

Then perform

xugaoxiang@ubuntu:~$ sudo systemctl enable rc-local Created symlink The/etc/systemd/system/multi - user. Target. Wants/rc - local. Service - > / etc/systemd/system/rc - local. Service.Copy the code

Then start the service and view its status

sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
Copy the code

The command output is as follows

Rc-local. service - /etc/rc.local Compatibility Loaded: Loaded (/etc/systemd/system/rc-local.service; enabled; └─debian.conf Active: Preset: enabled) Drop In: /lib/systemd/system/rc-local.service.d ├ ─debian.conf Active: active (running) since Thu 2018-11-01 13:17:08 CST; 2s ago Docs: man:systemd-rc-local-generator(8) Process: 10810 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS) Tasks: 1 (limit: 4915) CGroup: / system. Slice/rc - the local service └ ─ 10811 / usr/bin/python/usr/bin/sslocal - c/home/xugaoxiang/Tools/ss/ss. The json November 01 13:17:08 ubuntu systemd[1]: Starting /etc/rc.local Compatibility... Ubuntu Systemd [1]: Started /etc/rc.local Compatibility. 11月 01 13:17:08 Ubuntu rc.local Compatibility [10810]: INFO: Loading the config the from/home/xugaoxiang/Tools/ss/ss. Json November 01 13:17:08 ubuntu rc. Local [10810] : 2018-11-01 13:17:08 INFO loading libcrypto from libcrypto.so.1.1 Nov 01 13:17:08 Ubuntu rc.local[10810]: 2018-11-01 13:17:08 INFO loading libcrypto from libcrypto. 2018-11-01 13:17:08 INFO Starting local at 127.0.0.1:1080Copy the code

You can see that the script in rc.local has been executed correctly.