Linux开机自启/etc/init.d和/etc/rc.d/rc.local

发布于:2024-06-17 ⋅ 阅读:(25) ⋅ 点赞:(0)

/etc/init.d和/etc/rc.d/rc.local的区别

目的不同:
/etc/rc.d/rc.local:用于在系统启动后执行用户自定义命令,适合简单的启动任务。
/etc/init.d:用于管理系统服务的启动、停止和重启,提供更细粒度的服务管理。
执行时机:
rc.local 在所有其他服务启动完成后执行。
/etc/init.d 中的脚本根据运行级别在启动过程中按需执行。
复杂度和灵活性:
rc.local 更简单,适合不需要复杂管理的任务。
/etc/init.d 提供更强大的服务管理功能,适合需要精确控制的系统服务。
随着 systemd 的普及,新的系统通常使用 systemd 服务单元文件来替代传统的 init.d 脚本,但对 rc.local 的支持仍然保留作为一种便捷的方式来执行启动任务。

/etc/init.d

/etc/rc.d/rc.local 和 /etc/init.d 是 Linux 系统中与启动和服务管理相关的两个不同路径。它们在实现和用法上有显著差异。以下是它们的详细区别和各自用途:

  1. 服务管理脚本:/etc/init.d 目录包含了系统初始化脚本和服务管理脚本。这些脚本用于启动、停止、重启和检查系统服务的状态。
  2. 服务控制:每个脚本通常实现 start、stop、restart 和 status 等功能,用于管理特定服务。通过这些脚本,系统管理员可以方便地控制服务的生命周期。
  3. 与运行级别关联:这些脚本通常与系统的运行级别(runlevel)紧密关联。在传统的 SysVinit 系统中,不同的运行级别代表系统的不同状态(如单用户模式、多用户模式等),并决定哪些服务应该在启动时运行。
  4. 与 systemd 的关系:在现代使用 systemd 的系统中,/etc/init.d 目录仍然存在以保持向后兼容,但 systemd 的服务单元文件(*.service)已经成为管理服务的主要工具。
    示例服务脚本
    例如,一个服务脚本可能如下:
#!/bin/sh
# /etc/init.d/my_service

case "$1" in
    start)
        echo "Starting my_service"
        # Start command for my_service
        ;;
    stop)
        echo "Stopping my_service"
        # Stop command for my_service
        ;;
    restart)
        echo "Restarting my_service"
        # Restart command for my_service
        ;;
    status)
        echo "Checking status of my_service"
        # Status command for my_service
        ;;
    *)
        echo "Usage: /etc/init.d/my_service {start|stop|restart|status}"
        exit 1
        ;;
esac

exit 0

/etc/rc.d/rc.local
在很多 Linux 系统中,/etc/rc.d/rc.local 文件是一个用于在系统启动时执行自定义命令和脚本的文件。这种机制在传统的 SysVinit 系统中非常常见。虽然许多现代 Linux 系统已经转向使用 systemd,但仍有一些系统维护对 rc.local 的支持,或者通过兼容层来支持它。
以下是如何使用和编辑 /etc/rc.d/rc.local 文件来添加启动脚本的步骤:
主要特点

  1. 启动脚本:/etc/rc.d/rc.local 是一个脚本文件,用于在系统启动过程中执行用户自定义命令或脚本。它通常用于执行一些简单的初始化任务,如启动特定的应用程序、设置系统参数等。
  2. 执行时机:rc.local 脚本通常在所有其他初始化脚本和服务启动完成后才执行。这意味着它在系统的最后阶段运行,确保所有必要的系统服务和资源都已就绪。
  3. 兼容性:在使用 SysVinit 的传统 Linux 发行版中,这个文件比较常见。对于使用 systemd 的现代发行版,虽然直接支持 rc.local 的情况减少,但可以通过创建 rc-local 服务单元来继续使用。
    示例内容
#!/bin/sh
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full SysV style init stuff.

echo "Starting custom tasks"

# Custom commands or scripts
/path/to/custom/script.sh

exit 0
  1. 确保 rc.local 可用
    首先,确保系统支持并启用了 rc.local。在一些基于 systemd 的系统中,可能需要启用 rc-local 服务。
    检查 rc-local 服务状态
    sudo systemctl status rc-local
    如果服务未启用或不存在,你可以创建相应的服务文件:
    创建 rc-local 服务文件(适用于 systemd 系统)
  2. 创建文件 /etc/systemd/system/rc-local.service 并编辑:
    sudo nano /etc/systemd/system/rc-local.service
  3. 添加以下内容:
    [Unit]
    Description=/etc/rc.local Compatibility
    ConditionPathExists=/etc/rc.d/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

  1. 保存并退出编辑器。
  2. 使服务文件生效,并启用和启动 rc-local 服务:
    sudo chmod +x /etc/rc.d/rc.local
    sudo systemctl enable rc-local
    sudo systemctl start rc-local
  3. 编辑 rc.local 文件
  4. 打开 /etc/rc.d/rc.local 文件:
    sudo nano /etc/rc.d/rc.local
  5. 在文件中添加你希望在系统启动时执行的命令或脚本。在大多数系统中,你需要确保文件以 #!/bin/sh 开头,并在结尾返回退出状态码 0。例如:
#!/bin/sh
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full SysV style init stuff.

# Print the date to a log file
echo "System started at $(date)" >> /var/log/rc.local.log

# Start a custom service or script
/path/to/your/custom/script.sh

exit 0
  1. 保存并退出编辑器。
  2. 设置执行权限
    确保 /etc/rc.d/rc.local 文件具有可执行权限:
    sudo chmod +x /etc/rc.d/rc.local
    验证
    重新启动系统并检查是否执行了 rc.local 中的命令。例如,如果你将日志记录到 /var/log/rc.local.log,可以查看该日志文件以验证脚本是否正确执行。
    cat /var/log/rc.local.log
    通过这些步骤,你可以在系统启动时执行自定义脚本和命令,从而实现自动化配置和服务启动。尽管 systemd 已经成为主流,但对许多人来说,/etc/rc.d/rc.local 仍然是一个非常方便和熟悉的工具。

项目max-am实战
/etc/rc.d/rc.local

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

source /etc/profile

# ES start
su - es -c '/opt/elasticsearch-7.10.2/bin/elasticsearch -d'

# redis start
/opt/redis-7.0.12/redis_start.sh

# mysql start
/opt/mysql/bin/mysqld_safe &

# rabbitmq start
nohup /opt/rabbitmq_server-3.7.16/sbin/rabbitmq-server –detached > /dev/null &
# zookeeper start
/opt/zookeeper/zookeeper.sh start &

在这里插入图片描述

systemd介绍

systemd 是一个用于管理 Linux 操作系统中的系统和服务启动的系统和服务管理器。它被设计为取代传统的 SysVinit 系统,旨在提供更快的启动时间、并行启动能力和更好的系统管理功能。systemd 已经成为许多主流 Linux 发行版的默认初始化系统,包括 Red Hat Enterprise Linux、Fedora、Debian 和 Ubuntu 等。
主要特点
下面是 systemd 的一些主要特点:

  1. 并行启动:systemd 可以并行启动系统中的各个服务,而不像传统的 SysVinit 需要按顺序依次启动每个服务,从而大大缩短了启动时间。
  2. 自动化任务管理:systemd 使用单元文件来描述系统中的各种任务和服务,这些单元文件可以很容易地配置启动、停止、重启和状态检查等操作。
  3. 集成性:systemd 提供了广泛的功能,包括日志记录、用户会话管理、挂载点管理等,使得它成为一个全面的系统和服务管理解决方案。
  4. 可靠性:由于其对 Linux 系统的深度整合,systemd 能够更可靠地处理系统启动和服务管理,同时提供更多的故障排除和监控功能。
    systemd 单元
    在 systemd 中,系统中的各种任务和服务都被表示为单元。这些单元可以是服务单元(*.service)、挂载点单元(*.mount)、设备单元(*.device)、定时器单元(*.timer)等等。每个单元都有一组属性,用来描述其行为和依赖关系。
    与传统的 SysVinit 的比较
    相对于传统的 SysVinit,systemd 具有以下优势:
  • 更快的启动时间
  • 更好的并行性
  • 更灵活的任务和服务管理
  • 更强大的故障排除和监控能力
    总结
    在系统管理员和开发者的角度看,systemd 是一个功能强大、高效的系统和服务管理器,为 Linux 系统带来了许多现代化的特性和改进。它已经成为许多流行的 Linux 发行版的标准初始化系统,对于新的 Linux 系统来说,系统管理员通常需要熟悉并掌握 systemd 的使用和配置。