Skip to content

Instantly share code, notes, and snippets.

@Howard-Chang
Last active May 4, 2018 09:20
Show Gist options
  • Save Howard-Chang/0531fafebafb748ef42a10ed5fd50dcc to your computer and use it in GitHub Desktop.
Save Howard-Chang/0531fafebafb748ef42a10ed5fd50dcc to your computer and use it in GitHub Desktop.
linux auto shell
建立一個sh檔案,加入以下指令:
sh /home/es3/Downloads/elasticsearch-5.6.6/bin/elasticsearch &
sh /home/es3/Downloads/kibana-5.6.6-linux-x86_64/bin/kibana
cd /home/logstash/Downloads/logstash-5.6.6/bin
sh logstash -f /home/logstash/Downloads/logstash-5.6.6/bin/flow.conf
logstash server:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
systemctl stop ntopng &
systemctl stop firewalld &
systemctl stop nprobe &
sleep 5
nprobe /c --zmq "tcp://*:2055" -i none -n none --collector-port 5556 &
ntopng /c -i tcp://127.0.0.1:2055 -F "logstash;163.19.163.230;tcp;5510" --local-networks "163.19.0.0/16" &
cd /home/logstash/Downloads/logstash-5.6.6/bin
sh logstash -f /home/logstash/Downloads/logstash-5.6.6/bin/flow.conf
crontab -e
加入以下指令:
@reboot su logstash -c "sh /home/logstash/Downloads/test.sh"
參考文件:https://stackoverflow.com/questions/12973777/how-to-run-a-shell-script-at-startup
#!/bin/bash
# chkconfig: 345 99 01
# description: some startup script
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
. /etc/init.d/functions
SERVICE_NAME="ELK"
RETVAL=0
PID=-1
ntopngPID=-1
PIDFILE=/var/run/${SERVICE_NAME}.pid
ntopngFile=/var/run/ntopng.pid
start() {
# 首先检查PID文件是否已存在
if [ -f ${PIDFILE} ]; then
#kill -9 $PID
#kill -9 $ntopngPID
#rm -f $PIDFILE
#rm -f $ntopngFile
echo "PID file ${PIDFILE} already exists, please stop the service !"
exit
fi
echo "Starting service ${SERVICE_NAME} ..."
# >/dev/null 2>&1 表示不输出stdout和stderr
# 最后一个 & 表示整个命令在后台执行
#cd "/home/tao/"
su -c 'sh /home/logstash/Downloads/test.sh' >/dev/null 2>&1 &
PID=$! # 获取本shell启动的最后一个后台程序的进程号(PID)
if [ -z ${PID} ]; then # 检查有没有获取到pid
echo "Failed to get the process id, exit!"
exit
else
echo "Starting successfully, whose pid is ${PID}"
fi
touch $PIDFILE
echo ${PID} > ${PIDFILE}
}
stop() {
if [ -f $PIDFILE ]; then # 检查PIDFILE是否存在
PID=`cat ${PIDFILE}`
ntopngPID=`cat ${ntopngFile}`
if [ -z $PID ]; then # 检查PID是否存在于PIDFILE中
echo "PIDFILE $PIDFILE is empty !"
exit
fi
# 检查该进程是否存在
if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
echo "Process dead but pidfile exists!"
kill -9 $PID
kill -9 $ntopngPID
kill -9 $(($ntopngPID-1))
echo "Howard stop it successful! $(($ntopngPID-1)) "
rm -f $PIDFILE
rm -f $ntopngFile
exit
else
kill -9 $PID
kill -9 $ntopngPID
kill -9 $((ntopngPID-1))
echo "Stopping service successfully , whose pid is $PID"
rm -f $PIDFILE
rm -f $ntopngFile
fi
else
echo "File $PIDFILE does NOT exist!"
fi
}
restart() {
stop
start
}
status() {
# 检查pid file是否存在
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
# 检查pid file是否存在pid
if [ -z $PID ] ; then
echo "No effective pid but pidfile exists!"
else
# 检查pid对应的进程是否还存在
if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
echo "Process dead but pidfile exist"
else
echo "Running"
fi
fi
else
echo "Service not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: ELK.service {start|stop|restart|status}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment