说明
本文总结的是如何在RHEL6、RHEL7系统下,添加filebeat到系统服务的方法,为的是使用server和systemctl对filebeat进行管理。
RHEL7
编写服务配置文件
vim /etc/systemd/system/filebeat
[Unit]
Description=filebeat server daemon
Documentation=/usr/local/filebeat/filebeat -help
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Environment="BEAT_CONFIG_OPTS=-c /usr/local/filebeat/filebeat.yml"
ExecStart=/usr/local/filebeat/filebeat $BEAT_CONFIG_OPTS
Restart=always
[Install]
WantedBy=multi-user.target
设置开机自启
systemctl enable filebeat.service
RHEL6
编写程序控制脚本
vim /etc/init.d/filebeat
#!/bin/bash
#chkconfig 2345 10 90
#description: filebeatServer
agent="/usr/local/filebeat/filebeat"
args=" -c /usr/local/filebeat/filebeat.yml"
start() {
Pid=`ps -ef | grep filebeat.yml | grep -v grep | awk '{print $2}'`
if [ ! ${Pid} ];then
echo -n "Starting filebeat: "
${agent} ${args} > /usr/local/filebeat/run.log 2>&1 &
Pid=`ps -ef | grep filebeat.yml | grep -v grep | awk '{print $2}'`
if [ ! ${Pid} ];then
echo "start filebeat failed!!!"
exit 1
else
echo "start filebeat ok!!!"
fi
else
echo "filebeat is still running!!!"
fi
}
stop() {
Pid=`ps -ef | grep filebeat.yml | grep -v grep | awk '{print $2}'`
if [ ! ${Pid} ];then
echo "filebeat is not running!!!"
else
kill ${Pid}
echo "stop filebeat ok!!!"
fi
}
status() {
Pid=`ps -ef | grep filebeat.yml | grep -v grep | awk '{print $2}'`
if [ ! ${Pid} ];then
echo "filebeat is not running..."
else
echo "filebeat is running..."
fi
}
restart() {
stop
start
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: $0 [start|stop|restart|status]"
exit 1
esac
为控制脚本授权
chmod 775 /etc/init.d/filebeat
添加到系统服务
chkconfig --add /etc/init.d/filebeat
设置开机自启
chkconfig on filebeat