Centos7开机启动自定义脚本

本文详细介绍了在CentOS7环境下如何设置开机自启动程序,包括修改脚本权限、在rc.local中添加启动命令等步骤,以实现如nginx服务的自动启动。

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

linux开机自启动的方式跟window相似,也是把启动程序放在一个公共位置,随着开机自启动。

在CentOS 之前版本 ,把启动脚本放在/etc/rc.d/rc.local目录即可。但CentOS 7,/etc/rc.d/rc.local文件的权限被降低了,开机的时候执行自己的脚本是不能起动一些服务的。
查看下该文件的权限,就会知道,该文件没有执行权限。

[root@localhost ~]# cd /etc/rc.d/
[root@localhost rc.d]# ll
总用量 4
drwxr-xr-x. 2 root root  70 2月  27 23:30 init.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc0.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc1.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc2.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc3.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc4.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc5.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc6.d
-rw-r--r--. 1 root root 473 8月   5 2017 rc.local

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

而且查看rc.local文件,里面的注释也挺人性化,告诉你要一定要执行chmod +x /etc/rc.d/rc.local

[root@localhost rc.d]# cat 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
[root@localhost rc.d]#

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

实验

比如说:开机自动启动nginx服务(最近刚在虚拟机安装了一个nginx,拿来小试牛刀下)

先检查我当前nginx是未启动的

[root@localhost ~]# ps -ef|grep nginx
root       1238   1126  0 09:44 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]#

    1
    2
    3
    4

一、新建执行脚本hbk.sh

脚本的位置在/root/hbk目录下

[root@localhost hbk]# pwd
/root/hbk
[root@localhost hbk]# ll
总用量 35820
-rw-r--r--.  1 root root        40 4月  13 09:47 hbk.sh
drwxr-xr-x.  3 root root        53 4月  12 17:33 nginx
drwxr-xr-x. 11 root root       151 4月  13 08:51 nginx2
drwxr-xr-x. 10  502 games     4096 11月  2 02:52 zookeeper-3.4.11
-rw-r--r--.  1 root root  36668066 11月  9 02:24 zookeeper-3.4.11.tar.gz
[root@localhost hbk]# cat hbk.sh
#!/bin/bash
/root/hbk/nginx2/sbin/nginx
[root@localhost hbk]#

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

可以从上面看到hbk.sh没有执行权限。
二、使自启动程序拥有执行权限

[root@localhost hbk]# chmod +x hbk.sh
[root@localhost hbk]# ll
总用量 35820
-rwxr-xr-x.  1 root root        40 4月  13 09:47 hbk.sh
drwxr-xr-x.  3 root root        53 4月  12 17:33 nginx
drwxr-xr-x. 11 root root       151 4月  13 08:51 nginx2
drwxr-xr-x. 10  502 games     4096 11月  2 02:52 zookeeper-3.4.11
-rw-r--r--.  1 root root  36668066 11月  9 02:24 zookeeper-3.4.11.tar.gz
[root@localhost hbk]#

    1
    2
    3
    4
    5
    6
    7
    8
    9

三、在/etc/rc.d/rc.local中加入执行脚本命令,并设置执行权限。

在/etc/rc.d/rc.local文件末尾追加/root/hbk/hbk.sh

[root@localhost hbk]# vi /etc/rc.d/rc.local
[root@localhost hbk]# chmod +x /etc/rc.d/rc.local
[root@localhost hbk]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 492 4月  13 09:55 /etc/rc.d/rc.local
[root@localhost hbk]#

    1
    2
    3
    4
    5

简单的三步操作之后,重启下linux系统,验证下nginx服务已经开机启动了。
---------------------
作者:黄宝康
来源:优快云
原文:https://blog.youkuaiyun.com/huangbaokang/article/details/79924058
版权声明:本文为博主原创文章,转载请附上博文链接!

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

余额充值