如何在 Ubuntu 16.10 上启动时执行命令(rc.local 替代)

Sau*_*rma 57 systemd 16.10

我正在运行 Ubuntu 16.10 的 Linode 服务器上设置配额,但出现以下错误

无法 stat() 挂载设备 /dev/root: 没有那个文件或目录

所以为了解决这个问题,我到达了这个线程进行修复,这是通过添加

ln -s /dev/xvda /dev/root
/etc/init.d/quota restart
Run Code Online (Sandbox Code Playgroud)

/etc/rc.local. 但是 Ubuntu 16.10 不再使用rc.local,而是使用systemd。有什么替代方法rc.local,如何在启动时运行上述命令?

我也启用了该服务,systemctl enable rc-local.service但它对我不起作用。任何线索将不胜感激。

Jan*_*Jan 75

介绍

我认为您不应该按照 George 的链接中的建议创建新服务。本rc-local.service已存在于systemd和服务文件表明rc.local,如果存在且是可执行的,被自动拖入multi-user.target。因此,无需重新创建或强制执行systemd-rc-local-generator.

一种解决方案

一个快速的解决方案(我不知道这是否是规范的方式):

在终端中执行:

printf '%s\n' '#!/bin/bash' 'exit 0' | sudo tee -a /etc/rc.local
sudo chmod +x /etc/rc.local
sudo reboot
Run Code Online (Sandbox Code Playgroud)

之后rc.local将在系统启动时调用。插入你喜欢的。

背景

如果您在终端中执行以下操作:

sudo systemctl edit --full rc-local
Run Code Online (Sandbox Code Playgroud)

可以看到头部注释包含如下几行:

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
Run Code Online (Sandbox Code Playgroud)

这说明,在这个系统中,如果有一个叫做/etc/rc.local可执行文件的文件,那么它会被自动拉入multi-user.target。因此,您只需创建相应的文件 ( sudo touch...) 并使其可执行 ( sudo chmod +x ...)。

  • 不要使用 `#!/bin/bash`,除非你真的需要在你的脚本中使用 bash,否则使用 `#!/bin/sh`。出于性能原因,所有其他系统脚本都使用 dash,而不是 bash。 (4认同)

Geo*_*sen 24

我看到建议的这个解决方案涉及使用systemd 这里

  1. 创建服务:

    sudo vi /etc/systemd/system/rc-local.service
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在那里添加您的代码:

    [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
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建并确保/etc/rc.local可执行并将此代码添加到其中:

    sudo chmod +x /etc/rc.local

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    
    exit 0
    
    Run Code Online (Sandbox Code Playgroud)
  4. 启用服务:

    sudo systemctl enable rc-local
    
    Run Code Online (Sandbox Code Playgroud)
  5. 启动服务并检查状态:

    sudo systemctl start rc-local.service
    sudo systemctl status rc-local.service
    
    Run Code Online (Sandbox Code Playgroud)
  6. 如果一切顺利,您可以将您code/etc/rc.local文件添加到文件中,然后重新启动它。

注意:在 Lubuntu 16.10 上测试。

来源:

https://www.linuxbabe.com/linux-server/how-to-enable-etcrc-local-with-systemd


小智 6

添加到Jan 的回答中,与通常的rc.local文件不同,rc-local service它不是在所有服务启动后执行,而是在网络上线后执行。

在某些情况下,您可能希望rc.local稍后运行命令。例如,我希望它在lxd启动后执行。

在这种情况下,您可以rc-local service通过创建一个插入的 conf 文件来编辑启动依赖项: /etc/systemd/system/rc-local.service.d/override.conf 内容:

[Unit]
After=network.target lxd.service
Run Code Online (Sandbox Code Playgroud)

您可以在哪里添加所需的单位名称(就像我添加的那样lxd.service

之后不要忘记systemctl daemon-reload