CentOS的System V init启动脚本

本文深入解析了CentOS系统中SystemVinit服务脚本的结构与编写规范,涵盖了脚本的基本框架、常用函数及标签定义,如chkconfig与description等,适合于系统管理员和服务脚本开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CentOS系统本身自带了说明,在/usr/share/doc/initscripts-(*)/sysvinitfiles,内容如下:所有System V init脚本都命名为/etc/rc.d/init.d/<servicename>,其中</servicename><servicename>是服务的名称。必须没有“.init”后缀。

CentOS的System V init启动脚本CentOS的System V init启动脚本

示例脚本

#!/bin/bash
#
# /etc/rc.d/init.d/<servicename>
#
# <description of the *service*>
# <any general comments about this init script>
#
# <tags -- see below for tag definitions. *Every line* from the top
# of the file to the end of the tags section must begin with a #
# character. After the tags section, there should be a blank line.
# This keeps normal comments in the rest of the file from being
# mistaken for tags, should they happen to fit the pattern.>

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

<define any local shell functions used by the code that follows>

case "$1" in
    start)
        echo -n "Starting <servicename> services: "
        <start daemons, perhaps with the daemon function>
        touch /var/lock/subsys/<servicename>
    ;;
    stop)
        echo -n "Shutting down <servicename> services: "
        <stop daemons, perhaps with the killproc function>
        rm -f /var/lock/subsys/<servicename>
    ;;
    status)
        <report the status of the daemons in free-form format,
        perhaps with the status function>
    ;;
    restart)
        <restart the daemons, normally with $0 stop; $0 start>
    ;;
    reload)
        <cause the service configuration to be reread, either with
        kill -HUP or by restarting the daemons, possibly with
        $0 stop; $0 start>
    ;;
    probe)
        <optional. If it exists, then it should determine whether
        or not the service needs to be restarted or reloaded (or
        whatever) in order to activate any changes in the configuration
        scripts. It should print out a list of commands to give to
        $0; see the description under the probe tag below.>
    ;;
    *)
        echo "Usage: <servicename> {start|stop|status|reload|restart[|probe]"
        exit 1
    ;;
esac

注意:重启和重载功能可以(通常)组合成一个测试,

vis:

restart|reload)

 

不禁止您添加其他命令; 列出您打算以交互方式使用到使用消息的所有命令

/etc/rc.d/init.d/functions函数

daemon [+/-nicelevel] program [arguments] [&]

如果守护程序尚未运行,则启动该守护程序。还有其他一些有用的东西,例如,如果守护进程意外终止,则保留守护进程。

killproc program [signal]

向程序发送信号; 默认情况下,它发送一个SIGTERM,如果进程没有死,它会在几秒钟后发送一个SIGKILL。

如果找到pid文件,它还会尝试删除它。

pidofproc program

试图找到一个程序的pid; 检查可能的pidfiles,使用pidof程序,甚至使用ps。主要用于此文件中的其他函数,但也可用于脚本。

status program

打印状态信息。假设程序名称与servicename相同。

Tags.

# chkconfig: <startlevellist> <startpriority> <endpriority>

必须。是默认情况下应启动服务的级别列表。和是优先级编号。例如:

# chkconfig:2345 20 80有关详细信息,请阅读“man chkconfig”。

除非有一个非常好的,显性相反的原因,<endpriority>应该等于 100 - <startpriority>

# description: <multi-line description of service>

必须。几行描述,继续使用'\'字符。以下行中的初始注释和后续空格将被忽略。

# description[ln]: <multi-line description of service in the language \ # ln, whatever that is>

可选。应将描述翻译成指定的语言。

# processname:

可选,允许多个条目。对于脚本启动的每个进程名称,应该有一个进程名称条目。例如,samba服务启动两个守护进程:

#processname:smdb 
  #processname:nmdb

# config:

可选,允许多个条目。对于守护程序使用的每个静态配置文件,请使用单个条目。例如:

# config: /etc/httpd/conf/httpd.conf
  # config: /etc/httpd/conf/srm.conf

可选)如果服务器将自动重新加载配置文件(如果已更改),则可以在行中附加“autoreload”一词:

# config: /etc/foobar.conf autoreload

#pidfile:

可选,允许多个条目。使用就像配置条目一样,除了它指向pidfiles。假设pidfiles仅在进程创建时更新,而不是更晚。该文件的第一行应该是PID的ASCII表示; 终止换行符是可选的。不检查除第一行以外的任何行。

#project: true

可选,使用IN PLACE的processname,config和pidfile。如果存在,则可以通过运行以下命令来实现正确的重新加载 - 如果必要的循环:

command = $(/ etc / rd.d / init.d / SCRIPT probe)
[ -  n“$ command”] && /etc/rc.d/init.d/SCRIPT $ command

其中SCRIPT是服务的sysv init脚本的名称。

作为示例,需要执行复杂处理的脚本可以返回“run /var/tmp/<servicename.probe.$$”并实现“run”命令,该命令将执行命名脚本然后将其删除。

请注意,如果不需要执行任何操作使服务与其配置文件同步,则probe命令应该只是“exit 0”。

需要注意以下几点:

1、# chkconfig和# description不能少,必须写。

2、chkconfig的<startpriority> <endpriority>为启动优先级,在man中查询不到,一般end...不用理解,直接100-start...即可。start为开始的顺序,一般系统从小执行到大,数值任意,这个对于依赖启动有很大的帮助,比如控制先启动某个服务,再启动某个服务。以下是查询设置后的命令:

# 查询启动级别
chkconfig --list <servicename>
# 查询启动顺序
grep chkconfig /etc/rc.d/init.d/<servicenaem>

干货:《Linux就该这么学》

<think>我们参考了用户提供的四个引用,特别是引用[2]和引用[4]详细介绍了在CentOS7中设置shell脚本开机自启的方法。核心步骤是创建init.d脚本并使用chkconfig管理。需要注意的是,CentOS7虽然推荐使用systemd,但为了兼容性,传统的init.d方式仍然可用。根据引用[3]的示例,脚本需要特定的chkconfig配置行。具体步骤:1.编写shell脚本,放置在/etc/rc.d/init.d目录下,并在脚本开头添加chkconfig元数据(如运行级别、启动顺序)。2.赋予脚本可执行权限。3.使用chkconfig命令添加服务和设置开机启动。另外,引用[1]提到了设置开机自启动的目的是为了自动启动服务或执行初始化任务。用户的问题是在CentOS7中设置开机启动的shell脚本,因此我们将按照引用[2]和[4]的步骤详细说明。同时,考虑到用户可能是新手,步骤需要详细且清晰。以下是完整的配置步骤:</think> 在 CentOS 7 中配置 shell 脚本开机自启,主要通过 `systemd` 服务实现(推荐)或兼容传统的 `chkconfig` 方式。以下是具体方法: --- ### 📌 方法一:使用 systemd(推荐) 1. **创建服务文件** ```bash sudo vim /etc/systemd/system/your_service.service ``` 内容示例: ```ini [Unit] Description=自定义开机脚本服务 After=network.target # 在网络服务启动后执行 [Service] Type=forking ExecStart=/path/to/your_script.sh start # 脚本绝对路径 ExecStop=/path/to/your_script.sh stop Restart=on-failure [Install] WantedBy=multi-user.target # 多用户模式启动 ``` 2. **赋予脚本可执行权限** ```bash sudo chmod +x /path/to/your_script.sh ``` 3. **启用开机启动** ```bash sudo systemctl daemon-reload # 重载配置 sudo systemctl enable your_service # 设置开机自启 ``` 4. **验证服务状态** ```bash systemctl status your_service # 查看状态 ``` --- ### ⚙️ 方法二:使用 chkconfig(兼容传统) 1. **创建脚本init.d 目录** ```bash sudo vim /etc/rc.d/init.d/your_script ``` 脚本开头需包含(否则报错): ```sh #!/bin/sh #chkconfig: 2345 80 90 # 运行级别2~5,启动顺序80,停止顺序90 #description: 服务描述 ``` 2. **编写脚本逻辑** 参考模板: ```sh start() { echo "Starting service..." /path/to/your_script.sh } stop() { echo "Stopping service..." kill $(pgrep -f "your_script.sh") } case "$1" in start) start ;; stop) stop ;; restart) stop; start ;; *) echo "Usage: $0 {start|stop|restart}" esac ``` 3. **设置权限并启用** ```bash sudo chmod +x /etc/rc.d/init.d/your_script sudo chkconfig --add your_script # 添加到服务列表 sudo chkconfig your_script on # 启用开机启动 ``` 4. **验证配置** ```bash chkconfig --list | grep your_script ``` --- ### 🔍 关键注意事项 1. **脚本路径** 所有路径需用**绝对路径**(如 `/home/user/script.sh`),避免相对路径错误[^3][^4]。 2. **资源依赖** 若脚本依赖网络或数据库,需在服务文件中声明 `After=network.target mysqld.service`[^1]。 3. **日志管理** 建议在脚本中重定向输出(如 `>> /var/log/your_script.log 2>&1`),便于排查问题[^3]。 4. **环境变量** systemd 服务默认不加载用户环境变量,需在脚本内显式设置(如 `source /etc/profile`)。 --- > **操作建议**:优先使用 `systemd` 方式(方法一),因其日志监控、依赖管理更完善。对于简单脚本,传统方式(方法二)也可用但需注意 `chkconfig` 行不可省略[^2][^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值