centos7开机自启脚本

本文介绍如何在CentOS7系统中配置开机自启脚本,以实现自动启动redis、Tomcat、nginx、zookeeper等服务。通过创建启动脚本、授权、编辑rc.local文件等步骤,确保系统重启后服务能够自动运行。

目标:通过centos 7开机自启脚本来启动redis、Tomcat、nginx、zookeeper等服务.

第一步:创建你需要启动的脚本auto_start.sh(我这里只启动了redis)

#!/bin/sh
#redis启动
/home/zhouwen/redis/redis-5.0.0/src/redis-server /home/zhouwen/redis/redis-5.0.0/redis.conf

第二步:给脚本授权

chmod +x auto_start.sh

第三步:将脚本的运行路径添加到/etc/rc.d/rc.local中加入执行脚本命令。在/etc/rc.d/rc.local文件末尾追加/root/hbk/hbk.sh

#!/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
#这是我的启动脚本路径
/home/zhouwen/auto_start.sh

第四步:并设置 /etc/rc.d/rc.local 的执行权限

chmod +x /etc/rc.d/rc.local

简单的四步操作之后,重启下linux系统,验证下redis服务已经开机启动了。

 

<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、付费专栏及课程。

余额充值