Elastic: linux sets elasticsearch, kibana starts automatically

0 Preface

Every time you start the server, you have to manually start the es service, which is quite inconvenient. For this reason, write a script to realize the self-starting of es and kibana

1. Principle

First of all, for any service to start automatically at boot, it can be divided into the following three steps:
1. /etc/init.dCreate scripts for starting and closing services in the directory, and set the running level, startup priority, and shutdown priority in the script.

2. Authorize the script to ensure that the script can be executed

chmod +x /etc/init.d/xxx

3. Add the script to the boot list

# 添加开机自启
chkconfig --add xxx
# 状态设置为启动
chkconfig xxx on

Note: For some java services, you need to specify the jdk path, otherwise it will report an error that java cannot be found

2. Realize

1. Write startup script

cd /etc/init.d
vim elasticsearch

Script content

Among them elastic, it is a self-built non-root account, which is specially used to start es and kibana; su - elasticit means switching to the elastic account to execute

#!/bin/bash
#chkconfig: 2345 54 26
#description: elasticsearch
#processname: elasticsearch

ES_HOME=/var/local/elasticsearch

start(){
    
                               
        su - elastic -c "$ES_HOME/bin/elasticsearch -d -p pid"
        echo "es is started"
}
stop(){
    
                                    
        pid=`cat $ES_HOME/pid`
        kill -9 $pid
        echo "es is stopped"
}
status(){
    
    
        ps aux | grep $ES_HOME
}
restart(){
    
                  
        stop
        sleep 1
        start
}
case "$1" in        
"start")
        start      
        ;;
"stop")            
        stop
        ;;
"status")
        status
        ;;
"restart")            
        restart
        ;;
*)      
        echo "支持指令:$0 start|stop|restart|status"
        ;;
esac

2. Empower the script

chmod +x /etc/init.d/elasticsearch

3. Execute the script to verify

service elasticsearch status
service elasticsearch start
service elasticsearch stop

insert image description here

4. Add to the boot list

# 添加开机自启
chkconfig --add elasticsearch
# 状态设置为启动
chkconfig elasticsearch on

5. Write the startup script of kibana in the same way, and also execute the above empowerment and add boot list commands

vim kibana

Script content:
Because the ps command cannot obtain the kibana pid, the method of querying the process id through port 5601 is changed. If the subsequent port is adjusted, the script here must also be adjusted

#!/bin/bash
#chkconfig: 2345 55 27
#description: kibana
#processname: kibana

KIBANA_HOME=/var/local/kibana
start(){
    
                                    
        su - elastic -c "nohup $KIBANA_HOME/bin/kibana  >>/dev/null 2>&1 &"
        echo "kibana is started"
}
stop(){
    
                                    
        # 这里主要是通过网络端口5601寻找kibana进程的pid
        kibana_pid_str=`netstat -tlnp |grep 5601 | awk '{print $7}'`
        kibana_pid=`echo ${
     
     kibana_pid_str%%/*}`
        kill -9 $kibana_pid
        echo "kibana is stopped"
}
status(){
    
    
        ps aux | grep $KIBANA_HOME
}
restart(){
    
                  
        stop
        sleep 1
        start
}
case "$1" in        
"start")
        start      
        ;;
"stop")            
        stop
        ;;
"status")
        status
        ;;
"restart")            
        restart
        ;;
*)      
        echo "支持指令:$0 start|stop|restart|status"
        ;;
esac

6. Restart the server, access kibana directly after restarting, and the query is normal, indicating that both es and kibana are automatically started, and the settings are successful!

insert image description here

Guess you like

Origin blog.csdn.net/qq_24950043/article/details/132127736