Centos开机自启动脚本示例

本文建议创建一个sh文件管理自启动的各项内容,再将sh文件设置开机启动

在/root/autoshell下创建一个autostart.sh,内容如下

复制代码
#!/bin/bash
# description:开机自启脚本

# 启动mongodb
sh /root/software/mongodb-linux-x86_64-rhel70-4.0.6/bin/mongod --config /root/software/mongodb-linux-x86_64-rhel70-4.0.6/bin/mongodb.conf

# 启动springboot的一个服务
sh /root/projects/reboot-app.sh
  • PS

这个脚本后期可以灵活增删要自启动的内容

将上述脚本,加入开启自启动

1、赋予脚本可执行权限

chmod +x /root/autoshell/autostart.sh

2、打开/etc/rc.d/rc.local文件,在末尾增加如下内容

开机自启动脚本

sh /root/autoshell/autostart.sh

3、在centos7中,/etc/rc.d/rc.local的权限被降低了,所以需要执行如下命令赋予其可执行权限

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

springboot服务自启动脚本示例:reboot-app.sh

复制代码
kill -9 `ps aux|grep iotsite |grep -v grep |grep java |awk '{print $2}'`
sleep 1s
cd /root/projects
BUILD_ID=dontKillMe
source /etc/profile
nohup java -jar iotsite.jar --spring.profiles.active=test --server.port=8180 >/dev/null 2>&1&
sleep 1s

此外还有systemctl方式,允许 RabbitMQ 和 Nginx 开机自启动

1 启用 RabbitMQ 开机自启动:

cd /usr/lib/systemd/system/

systemctl enable rabbitmq-server.service

这条命令会创建一个符号链接,确保在系统启动时自动运行 RabbitMQ 服务。

验证 RabbitMQ 服务开机自启动设置:

为了确认 RabbitMQ 服务的开机自启动设置已经成功,您可以使用以下命令检查服务文件的状态:

systemctl list-unit-files | grep rabbitmq

如果设置成功,您应该能在输出中看到 rabbitmq-server.service 及其状态(通常是 enabled)。

2 启用 Nginx 开机自启动:

同样地,您可以使用 systemctl 命令启用 Nginx 服务的开机自启动功能。

systemctl enable nginx.service

验证 Nginx 服务开机自启动设置:

使用类似的命令来检查 Nginx 服务的开机自启动设置:

systemctl list-unit-files | grep nginx

如果设置成功,您应该能在输出中看到 nginx.service 及其状态(同样是 enabled)。

相关推荐
笑衬人心。1 小时前
初学Spring AI 笔记
人工智能·笔记·spring
StephenCurryFans1 小时前
Spring AI vs LangChain4j:Java AI开发框架完整对比指南 🚀
后端·spring
编程小白gogogo2 小时前
Spring学习笔记
笔记·学习·spring
好名字更能让你们记住我2 小时前
Linux多线程(十二)之【生产者消费者模型】
linux·运维·服务器·jvm·windows·centos
harmful_sheep2 小时前
Spring 为何需要三级缓存解决循环依赖,而不是二级缓存
java·spring·缓存
崎岖Qiu4 小时前
【Spring篇08】:理解自动装配,从spring.factories到.imports剖析
java·spring boot·后端·spring·面试·java-ee
述雾学java6 小时前
Spring Cloud 服务追踪实战:使用 Zipkin 构建分布式链路追踪
分布式·spring·spring cloud·zipkin
MonkeyKing_sunyuhua11 小时前
Ehcache、Caffeine、Spring Cache、Redis、J2Cache、Memcached 和 Guava Cache 的主要区别
redis·spring·memcached
考虑考虑11 小时前
@FilterRegistration和@ServletRegistration注解
spring boot·后端·spring
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos