centos7使用Shell脚本设置开机自启

centos7使用Shell脚本设置开机自启

一、创建shell脚本文件
touch /etc/init.d/AutoRunJava.sh
二、编写shell脚本
#!/bin/bash
#chkconfig: 35 20 80
#description: http server


export datacollect=/appdata/data_collection_analyze_server/aoya_data_collection_analyze_server-0.0.1-SNAPSHOT.jar
export datapush=/appdata/datapush/aoya_datapush-0.0.1-SNAPSHOT.jar
export aoya=/appdata/aoya/aoya-1.5.jar
 
export datacollect_port=8345
export datapush_port=8877
export aoya_port=7776
 
echo "--------data_collect开始启动--------------"
nohup java -jar $datacollect > /dev/null 2>&1 &
datacollect_port=`lsof -i:$datacollect_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "datacollect_port" ]
            do
              datacollect_port=`lsof -i:$datacollect_port|grep "LISTEN"|awk '{print $2}'`  
            done
echo "datacollect_port pid is $datacollect_port" 
echo "--------datacollect_port 启动成功--------------"


echo "--------datapush开始启动--------------"
nohup java -jar $datapush > /dev/null 2>&1 &
datapush_pid=`lsof -i:$datapush_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "$datapush_pid" ]
            do
              datapush_pid=`lsof -i:$datapush_port|grep "LISTEN"|awk '{print $2}'`  
            done
echo "datapush pid is $datapush_pid" 
echo "--------datapush 启动成功--------------"



echo "--------aoya开始启动--------------"
nohup java -jar $aoya > /dev/null 2>&1 &
aoya_pid=`lsof -i:$aoya_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "$tongda_pid" ]
            do
              tongda_pid=`lsof -i:$aoya_port|grep "LISTEN"|awk '{print $2}'`  
            done
echo "aoya pid is $aoya_pid" 
三、设置脚本的可执行权限
chmod u+x /etc/init.d/AutoRunJava.sh
四、添加脚本到开机自动启动项目中
cd /etc/init.d
chkconfig --add  AutoRunJava.sh
五、查看是否执行成功:
chkconfig --list AutoRunJava.sh

AutoRunJava.sh 0:off 1:off 2:on 3:on 4:on 5:on 6:off

表示已生效

如果0-6全为off则表示未生效,此时执行下面的命令:

chkconfig AutoRunJava.sh on
六、reboot重启
<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、付费专栏及课程。

余额充值