服务器开机自启服务 rc-local 相关配置总结
如果需要在 Ubuntu 系统中使用 rc-local,需要手动创建三个文件,分别是 rc.local、rc.local.service和 rc-local.service。
rc.local、rc.local.service和rc-local.service都用于在Linux系统开机时运行自定义命令或脚本的目的。在不同的Linux发行版和版本中,具体实现可能会有所不同。
一般来说,rc.local文件位于/etc/目录中,可以用于在开机时运行自定义命令或脚本。该文件在许多Linux发行版中都被使用,包括Ubuntu。
然而,一些Linux发行版,包括最近版本的Ubuntu,使用systemd作为他们的init系统,而不是upstart。在这些情况下,rc.local文件可能不会被默认执行,需要使用rc-local.service文件替代。这个服务用于在systemd下在开机时执行/etc/rc.local文件。可以使用systemctl命令启用和启动此服务。
总之,rc.local、rc.local.service和rc-local.service都用于相同的目的,但具体实现可能会因使用的Linux发行版和版本而有所不同。
rc.local 文件配置示例:
此文件存放目录为 /etc
#!/bin/bash -e
# 启动Supervisor守护进程
cd /home/test/soft
sudo bash mkdir_supervisor.sh
cd /home/test/super
sudo /home/test/miniconda3/envs/utils_env/bin/supervisord -c supervisord.conf
exit 0
rc.local.service 文件配置示例:
此文件存放目录为 /etc/systemd/system
# 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.target
Alias=rc-local.service
rc-local.service 文件配置示例:
此文件存放目录为 /etc/systemd/system
# 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.target
Alias=rc-local.service
上述文件配置完成后,需要给予文件可执行权限,并配置开机自启服务,相关命令如下:
# 配置rc.local可执行权限
chmod +x /etc/rc.local
chmod 0644 /etc/systemd/system/rc-local.service
# 启用开机自启服务
systemctl enable rc-local
systemctl start rc-local.service