我正在使用AWS EC2和需要运行的自定义脚本:
java -jar logstash-1.3.3-flatjar.jar agent -f shipper.conf
但它似乎只有在我进入服务器并手动运行后才能工作。 我找到了这个链接:“Linux Launch java program on startup (EC2 instance)”
但这对我没有帮助。如何让我的bash脚本运行此命令?我甚至尝试创建第二个只执行我的logstash java脚本的shell脚本。
答案 0 :(得分:0)
无需编写自己的initscript,logstash网站provides代码。复制/etc/init.d/logstash
中的脚本,然后运行chkconfig --add logstash
和chkconfig logstash on
启用它;那么您可以像往常一样使用service
命令控制它。
请注意,您需要编辑环境变量配置部分以适应您的系统。
#! /bin/sh
#
# /etc/rc.d/init.d/logstash
#
# Starts Logstash as a daemon
#
# chkconfig: 2345 20 80
# description: Starts Logstash as a daemon
# pidfile: /var/run/logstash-agent.pid
### BEGIN INIT INFO
# Provides: logstash
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Logstash
# Description: Starts Logstash as a daemon.
# Modified originally from https://gist.github.com/2228905#file_logstash.sh
### END INIT INFO
### CONFIGURATION
# Amount of memory for Java
#JAVAMEM=256M
# Location of logstash files
LOCATION=/opt/logstash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DESC="Logstash Daemon"
NAME=java
DAEMON=$(which java)
CONFIG_DIR=/opt/logstash/logstash.conf
LOGFILE=/opt/logstash/logstash.log
JARNAME=logstash-monolithic.jar
#ARGS="-Xmx$JAVAMEM -Xms$JAVAMEM -jar ${JARNAME} agent --config ${CONFIG_DIR} --log ${LOGFILE} --grok-patterns-path ${PATTERNSPATH}"
ARGS="-jar ${JARNAME} agent --config ${CONFIG_DIR} --log ${LOGFILE}"
SCRIPTNAME=/etc/init.d/logstash
PIDFILE=/var/run/logstash.pid
base=logstash
### END CONFIGURATION
# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then
{
echo "Couldn't find $DAEMON"
exit 99
}
fi
. /etc/init.d/functions
#
# Function that starts the daemon/service
#
do_start()
{
cd $LOCATION && \
($DAEMON $ARGS &) \
&& success || failure
}
set_pidfile()
{
pgrep -f "$DAEMON[[:space:]]*$ARGS" > $PIDFILE
}
#
# Function that stops the daemon/service
#
do_stop()
{
pid=`cat $PIDFILE`
if checkpid $pid 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid >/dev/null 2>&1
usleep 100000
if checkpid $pid && sleep 1 &&
checkpid $pid && sleep $delay &&
checkpid $pid ; then
kill -KILL $pid >/dev/null 2>&1
usleep 100000
fi
fi
checkpid $pid
RC=$?
[ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
}
case "$1" in
start)
echo -n "Starting $DESC: "
do_start
touch /var/lock/subsys/$JARNAME
set_pidfile
;;
stop)
echo -n "Stopping $DESC: "
do_stop
rm /var/lock/subsys/$JARNAME
rm $PIDFILE
;;
restart|reload)
echo -n ""Restarting $DESC: "
do_stop
do_start
touch /var/lock/subsys/$JARNAME
set_pidfile
;;
status)
status -p $PIDFILE
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
exit 3
;;
esac
echo
exit 0