目录
一、背景需求
编译完zabbix-agent后,生成sbin/zabbix_agentd启动命令,需求是希望zabbix_agentd以服务的形式启动,并能开机自启动。
二、实现方法
1、查看自启动脚本
在源码包路径下,有脚本文件:
/usr/local/src/zabbix/misc/init.d/fedora/core5/zabbix_agentd
查看脚本文件内容命令:
cat zabbix_agentd
[root@localhost tru64]# cat zabbix_agentd
#!/bin/bash
# /etc/rc.d/init.d/zabbix_agentd
# Starts the zabbix_agentd daemon
# chkconfig: - 95 5
# description: Zabbix Monitoring Agent
# processname: zabbix_agentd
# pidfile: /tmp/zabbix_agentd.pid
# Modified for Zabbix 2.0.0
# May 2012, Zabbix SIA
# Source function library.
. /etc/init.d/functions
RETVAL=0
prog="Zabbix Agent"
ZABBIX_BIN="/usr/local/sbin/zabbix_agentd"
if [ ! -x ${ZABBIX_BIN} ] ; then
echo -n "${ZABBIX_BIN} not installed! "
# Tell the user this has skipped
exit 5
fi
start() { echo -n $"Starting $prog: "
daemon $ZABBIX_BIN
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/zabbix_agentd
echo}
stop() { echo -n $"Stopping $prog: "
killproc $ZABBIX_BIN
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zabbix_agentd
echo}
case "$1" in start)
start ;; stop)
stop ;; reload|restart)
stop sleep 10 start
RETVAL=$?;; condrestart)
if [ -f /var/lock/subsys/zabbix_agentd ]; then
stop start fi ;; status)
status $ZABBIX_BIN RETVAL=$? ;; *)
echo $"Usage: $0 {condrestart|start|stop|restart|reload|status}"
exit 1
esac
exit $RETVAL
2、将启动脚本拷贝到/etc/init.d目录
源码包在/usr/local/src/目录,执行拷贝命令:
cp /usr/local/src/zabbix/misc/init.d/fedora/core5/zabbix_agentd /etc/init.d/
3、修改zabbix_agentd启动脚本
将DAEMON启动命令路径修改为安装时指定的路径。
实际环境中DAEMON是引用ZABBIX_BIN
所以修改文件内容为:ZABBIX_BIN=”/usr/local/zabbix-agent/sbin/zabbix_agentd”
4、启动zabbix_agentd服务
执行服务启动命令:
/etc/init.d/zabbix_agentd start
出现警告:Warning: The unit file, source configuration file or drop-ins of zabbix_agentd.service changed on disk. Run 'systemctl daemon-reload' to reload units.
解决方法:执行systemctl daemon-reload赋予执行权限即可
5、设置开机自动启动
设置开机自启动命令:
systemctl enable zabbix_agentd
出现提示:zabbix_agentd.service is not a native service, redirecting to systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable zabbix_agentd
解决方法:执行/usr/lib/systemd/systemd-sysv-install enable zabbix_agentd即可。