Linux 设置开机自启动脚本(ES、MySQL、Nacos、Nginx)
- cd /etc/rc.d/init.d;新建startupOperation.sh文件。
- chmod +x startupOperation.sh,赋予可执行权限,此时文件会变色。
- chkconfig --add startupOperation.sh
- chkconfig startupOperation.sh on
- sudo reboot 重启后,确认是否成功
若启动失败,则去/var/log/boot.log查找启动日志,查看失败原因;也可systemctl status {脚本文件名字}.service查看,我这里是systemctl status startupOperation.service
#!/bin/sh #chkconfig: 2345 80 90 #description:auto_run echo "启动es" ES_PID=`ps -ef | grep elasticsearch | grep -v grep | awk '{print $2}'` if [ ! -z "$ES_PID" ] ; then echo "es is runing...pid:$ES_PID" else echo "start es" cd '/data/tools/elasticsearch-7.3.0/bin' ph=`pwd` echo "$ph" su - elasticsearch -c 'sh /data/tools/elasticsearch-7.3.0/bin/elasticsearch -d;exit' fi echo "启动nacos" NACOS_PID=`ps -ef | grep nacos | grep -v grep | awk '{print $2}'` if [ ! -z "$NACOS_PID" ] ; then echo "nacos is runing...pid:$NACOS_PID" else echo "start nacos" cd /data/nacos/bin nohup sh startup.sh -m standalone & fi echo "启动redis" REDIS_PID=`ps -ef | grep redis | grep -v grep | awk '{print $2}'` if [ ! -z "$REDIS_PID" ] ; then echo "redis is runing...pid:$REDIS_PID" else echo "start REDIS" cd /data/tools/redis-4.0.2/src redis-server ../redis.conf fi echo "启动nginx" NGINX_PID=`ps -ef | grep nginx | grep -v grep | awk '{print $2}'` if [ ! -z "$NGINX_PID" ] ; then echo "nginx is runing...pid:$NGINX_PID" else echo "NGINX REDIS" /usr/local/nginx/sbin/nginx fi echo "启动MySQL" MYSQL_IS_START=`netstat -lntup |grep 3306|wc -l` if [ $MYSQL_IS_START -eq 1 ] ; then echo "mysql is runing..." else echo "start mysql" service mysqld start fi
注:上面的三行是中,第二,第三行是必须的,否则在运行chkconfig --add时,会报错!!!