• 如何启用/etc/rc。本地与Systemd

    如果您正在运行一个使用Systemd的Linux发行版,那么您可能会在/etc/rc中找到您的命令。本地文件不会在系统启动时运行。本指南介绍了如何启用/etc/rc。要在系统启动时运行的本地脚本。 启用/etc/rc。系统本地 如果在终端中键入以下命令: sudo systemctl status rc-local 您可能会得到以下输出: ● 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. 如果您尝试启用/etc/rc。使用以下命令在系统启动时运行本地: sudo systemctl enable rc-local 你可能会得到: 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, ...). 解决方案 从上面可以看到,单元文件没有[Install]部分。因此,Systemd无法启用它。首先,我们需要创建一个文件: sudo nano /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 保存并关闭文件。要在Nano文本编辑器中保存文件,请按Ctrl+O,然后按Enter确认。要退出文件,请按Ctrl+X。下一步,运行以下命令以确保/etc/rc。本地文件是可执行的。 sudo chmod +x /etc/rc.local 注意:从16.10开始,Ubuntu不附带/etc/rc。本地文件了。可以通过执行此命令来创建文件。 printf '%s/n' '#!/bin/bash' 'exit 0' | sudo tee -a /etc/rc.local 然后将执行权限添加到/etc/rc。本地文件。 sudo chmod +x /etc/rc.local 之后,在系统启动时启用服务: sudo systemctl enable rc-local 输出: Created symlink from /etc/systemd/system/multi-user.target.wants/rc-local.service to /etc/systemd/system/rc-local.service. 现在启动服务并检查其状态: sudo systemctl start rc-local.service sudo systemctl status rc-local.service 输出: ● 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 Cron@reboot 如果上述方法不适用于您,或者您只是希望在系统引导时执行一些简单的命令,那么您还可以使用cron中的@reboot功能在系统引导时自动执行命令。例如,我希望我的shadowsocks客户端自动启动,因此我打开根用户的cron文件: sudo crontab -e 然后把下面的一行放在它的末尾。 @reboot /usr/bin/sslocal -c /etc/shadowsocks.json -d start 保存并关闭文件。 在一些Linux发行版(如archlinux)中,默认情况下不启用cron守护进程。所以你必须手动启用它。要在archlinux上启用它,请在终端中输入以下命令。 sudo systemctl enable cronie Shadowsocks是一个socks5代理,可用于绕过Internet防火墙。如果您感兴趣,请单击下面的链接,了解如何设置自己的Shadowsocks服务器。 在Debian、Ubuntu和Centos上安装自己的shadowsocks服务器 如何使用Systemd 想了解更多有关systemd的信息以有效管理您的系统吗?请阅读以下教程。 如何在Linux上使用Systemd管理服务、运行级别和日志

    2022.03.24 浏览:103