Linux | 开机自启动配置/启动脚本

1. Ubuntu (命令:sysv-rc-conf)

1.1 安装 sysv-rc-conf

1.1.1 修改资源下载地址

sudo vim /etc/apt/sources.list

##添加到最后一行 保存就行
deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse

在这里插入图片描述

1.1.2 安装/验证

安装
会有一个选择 yes/no的 yes 就完了

sudo apt-get update
sudo apt-get install sysv-rc-conf

验证

sysv-rc-conf --list

在这里插入图片描述

1.2 准备脚本

这里准备了 Tomcat、 Minio、Elasticsearch 的脚本,根据自己需求修改. 启中包含了 start、stop、 restart 三个方法

–> 将脚本放到 /etc/init.d 文件下 <–

看脚本注释

1.2.1 Tomcat 脚本

#!/bin/bash
#description: tomcat
#processname: tomcat-8.5.81
export TOMCAT_HOME=/opt/project/apache-tomcat-8.5.81/ #安装位置 改成自己的

case $1 in
        start)
                cd $TOMCAT_HOME
				./bin/startup.sh
                echo "tomcat is started"
                ;;
        stop)
        		# apache-tomcat-8.5.81 改成自己的
                ps -ef | grep apache-tomcat-8.5.81 | grep -v grep | awk '{print $2}' | xargs kill
                echo "tomcat is stopped"
                ;;
        restart)
				ps -ef | grep apache-tomcat-8.5.81 | grep -v grep | awk '{print $2}' | xargs kill
                echo "tomcat is stopped"
                sleep 1
				cd $TOMCAT_HOME
				./bin/startup.sh
				./bin/startup.sh
                echo "tomcat is started"
        ;;
    *)
        echo "start|stop|restart"
        ;;
esac
exit 0

1.2.2 Minio 脚本

#!/bin/bash
#description: minio
#processname: minio
export MINIO_HOME=/opt/minio/ #安装位置

case $1 in
        start)
                cd $MINIO_HOME
				export MINIO_ACCESS_KEY=zhkj
				export MINIO_SECRET_KEY=zhkj2501
				nohup ./minio server /opt/minio/data --address=0.0.0.0:9000 1>minio.log 2>&1 &
                echo "minio is started"
                ;;
        stop)
                ps -ef | grep minio | grep -v grep | awk '{print $2}' | xargs kill
                echo "minio is stopped"
                ;;
        restart)
                ps -ef | grep minio | grep -v grep | awk '{print $2}' | xargs kill
                echo "minio is stopped"
                sleep 1
				cd $MINIO_HOME
				export MINIO_ACCESS_KEY=zhkj
				export MINIO_SECRET_KEY=zhkj2501
				nohup ./minio server /opt/minio/data --address=0.0.0.0:9000 1>minio.log 2>&1 &
                echo "minio is started"
        ;;
    *)
        echo "start|stop|restart"
        ;;
esac
exit 0

1.2.3 Elasticsearch 脚本

#!/bin/bash
#description: ElasticSearch
#processname: ElasticSearch
export ES_HOME=/opt/es/ #ES安装位置

case $1 in
        start)
        	## 防止安装目录出现root 权限的文件 换成自己创建的账户
			chown -R zhkj:zhkj $ES_HOME
			## es 不能使用ROOT 账户启动 需要换成自己创建的账户
			## 我这里是单机 部署了三个 所以启动三个 跟启动kibana  sleep是等待1秒
			su zhkj<<!
				cd $ES_HOME
				./elasticsearch-node1/bin/elasticsearch -d
				./elasticsearch-node2/bin/elasticsearch -d
				./elasticsearch-node3/bin/elasticsearch -d
				sleep 1
				nohup ./kibana-7.3.1-linux-x86_64/bin/kibana 1>/opt/es/kibana.log 2>&1 &
				exit
!
;;
        stop)
                ps -ef | grep elasticsearch | grep -v grep | awk '{print $2}' | xargs kill
				ps -ef | grep kibana | grep -v grep | awk '{print $2}' | xargs kill
                echo "elasticsearch is stopped"
                ;;
        restart)
                ps -ef | grep elasticsearch | grep -v grep | awk '{print $2}' | xargs kill
				ps -ef | grep kibana | grep -v grep | awk '{print $2}' | xargs kill
                echo "elasticsearch is stopped"
				sleep 1
					su zhkj<<!
					cd $ES_HOME
					./elasticsearch-node1/bin/elasticsearch -d
					./elasticsearch-node2/bin/elasticsearch -d
					./elasticsearch-node3/bin/elasticsearch -d
					sleep 1
					nohup ./kibana-7.3.1-linux-x86_64/bin/kibana 1>/opt/es/kibana.log 2>&1 &
					exit
!
        ;;
    *)
        echo "start|stop|restart"
        ;;
esac
exit 0

1.2.4 授启动权限/验证脚本

## 授权
chmod 777 elasticsearch

chmod 777 tomcat

chmod 777 minio

## 验证
./elasticsearch
./tomcat
./minio

出现下图这样说明脚本没有问题 如有有问题 就看 1.4 是否能解决
在这里插入图片描述

1.3 配置/验证

1.3.1 sysv-rc-conf 配置自启

 sysv-rc-conf elasticsearch on
 sysv-rc-conf tomcat on
 sysv-rc-conf minio on

配置完成后可以通过 命令查看是否配置生效

sysv-rc-conf --list 

#或者
sysv-rc-conf --list |grep tomcat
sysv-rc-conf --list |grep elasticsearch 
sysv-rc-conf --list |grep minio 

在这里插入图片描述
立即重启验证

 shutdown -r now

1.4 可能出现问题

如果提示 脚本的错误 需要自己看 是脚本哪里写错了

如果是以下错误 解释器错误 那么是格式的问题
在这里插入图片描述
问题: windows系统下编写,linux系统下运行。windows下的.sh文件的格式为dos格式,和Linux只能执行unix格式的脚本。

解决方案: vim 脚本 输入

:set ff=unix    #回车 在 :wq 保存退出就可以

在这里插入图片描述
然后重新执行 就解决格式问题了

2. Centos (命令: chkconfig)

2.1 准备脚本

跟以上的脚本一样 请查看 目录 1.2

1.3 配置

#查看自启列表
chkconfig --list

#配置后再查看
chkconfig tomcat on
chkconfig minio on
chkconfig elasticsearch on
##或者使用 --add 也可以

## 然后查看列表是否存在
chkconfig --list

##重启验证
shutdown -r now
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一介草民丶

谢谢老板的一分钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值