如何在启动时启动 /etc/init.d 中的脚本?

Naf*_*Kay 104 fedora boot init-script

我想我很久以前读过一些关于这个的东西,但我不记得它是如何完成的。本质上,我有一个服务/etc/init.d,我想在启动时自动启动。我记得这与将脚本符号链接到/etc/rc.d目录有关,但我现在不记得了。这是什么命令?

我相信我在使用 Fedora/CentOS 衍生产品。

can*_*nen 133

如果您使用的是基于 Red Hat 的系统,正如您所提到的,您可以执行以下操作:

  1. 创建一个脚本并放入/etc/init.d(例如/etc/init.d/myscript)。该脚本应具有以下格式:
#!/bin/bash
# chkconfig: 2345 20 80
# description: Description comes here....

# Source function library.
. /etc/init.d/functions

start() {
    # code to start app comes here 
    # example: daemon program_name &
}

stop() {
    # code to stop app comes here 
    # example: killproc program_name
}

case "$1" in 
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       # code to check status of app comes here 
       # example: status program_name
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0 
Run Code Online (Sandbox Code Playgroud)

格式非常标准,您可以在/etc/init.d. 然后,您可以像 so/etc/init.d/myscript start或一样使用脚本chkconfig myscript start。该ckconfig手册页介绍了剧本的标题:

 > This says that the script should be started in levels 2,  3,  4, and
 > 5, that its start priority should be 20, and that its stop priority
 > should be 80.
Run Code Online (Sandbox Code Playgroud)

示例启动、停止和状态代码使用定义在 /etc/init.d/functions

  1. 启用脚本

    $ chkconfig --add myscript 
    $ chkconfig --level 2345 myscript on 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 检查脚本确实已启用 - 您应该看到所选级别的“开启”。

    $ chkconfig --list | grep myscript
    
    Run Code Online (Sandbox Code Playgroud)

  • Chkconfig 就是你想要的。+1 (4认同)

use*_*own 15

您测试您的机器通常启动的运行级别。

runlevel
Run Code Online (Sandbox Code Playgroud)

通常这是 5 或 2 - 有各种约定,但没有真正建立,afaik。Ubuntu 使用 2,而我以前使用的发行版总是使用

  • 1 单用户(超级用户)
  • 2 多用户
  • 3 多用户+网络
  • 4 未使用/用户可定​​义
  • 5个多用户,网络+X11

然后,你从你的初始化脚本做一个符号链接,也许/etc/init.d/foobar/etc/rc2.d/SXYfoobar

S 表示'在此运行级别中启动此脚本(此处为:2)。XY 是两位十进制数,与序列相关,脚本启动。

如果您依赖脚本 S45barfoo 在您之前运行,而 S55foofoo 依赖于您的脚本,您将选择 xy 介于 45 和 55 之间。对于相同的数字,引导顺序未定义。

Ubuntu 同时切换(正在切换)到另一个启动程序,称为upstart.

并注意:链接并不总是链接到/etc/rcX.d- 有时它/etc/init/rcX.d或类似的东西,但应该很容易找到,在 /etc 下的某个地方。

如果你想在启动脚本的末尾启动一些东西,/etc/rc.local将是要查找的文件,但如果它依赖于已经在运行的 X11,你可能会寻找桌面环境的自动启动选项,或者/etc/X11/Xsession.d/具有与描述类似的模式以上。

如果您依赖网络正常运行,则有一个单独的目录 (if-up.d),以及用于安装的设备,如外部 USB 驱动器/etc/udev/rules.d/

  • 不是`update-rc.d`吗? (3认同)

Ste*_*uan 5

在这个答案中,假设我想通过 Ubuntu 系统上的服务脚本挂载在/etc/fstabvia中定义的 Windows 共享 (CIFS)。mount -a -t cifs首先,我在下面创建一个/etc/init.d/mountcifs包含内容的条目:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          mountcifs
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Mounts / Umounts Window Shares (CIFS)
# Description: Mounts / Umounts Window Shares (CIFS)
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/lsb/init-functions

case "$1" in
  start)
        mount -a -t cifs
        ;;
  stop)
        umount -a -t cifs
        ;;
  restart|reload|force-reload)
        umount -a -t cifs
        mount -a -t cifs
        ;;
  *)
        # echo "Usage: $0 start|stop" >&2
        # exit 3
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

确保/etc/init.d/mountcifs可以使用chmod 755 /etc/init.d/mountcifs.

请注意# Default-Start: 2 3 4 5评论中的。由于这个注释,我们可以使用以下命令让 Ubuntu 在、 和文件夹/etc/rc2.d/etc/rc3.d创建符号链接:/etc/rc4.d/etcrc5.d

#! /bin/sh
### BEGIN INIT INFO
# Provides:          mountcifs
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Mounts / Umounts Window Shares (CIFS)
# Description: Mounts / Umounts Window Shares (CIFS)
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/lsb/init-functions

case "$1" in
  start)
        mount -a -t cifs
        ;;
  stop)
        umount -a -t cifs
        ;;
  restart|reload|force-reload)
        umount -a -t cifs
        mount -a -t cifs
        ;;
  *)
        # echo "Usage: $0 start|stop" >&2
        # exit 3
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

然后我们可以测试脚本如下:

sudo update-rc.d mountcifs defaults
Run Code Online (Sandbox Code Playgroud)

Windows 共享应该在重新启动时自动安装。