2008-12-08 06:42:56 +00:00
|
|
|
|
#! /bin/sh
|
|
|
|
|
### BEGIN INIT INFO
|
|
|
|
|
# Provides: @DAEMON@
|
2011-01-31 11:25:50 +00:00
|
|
|
|
# Default-Start: 2
|
2008-12-08 06:42:56 +00:00
|
|
|
|
# Default-Stop: 1
|
|
|
|
|
# Short-Description: Yandex daemon
|
|
|
|
|
### END INIT INFO
|
|
|
|
|
|
2011-03-04 14:16:45 +00:00
|
|
|
|
USER=metrika
|
|
|
|
|
GROUP=metrika
|
2008-12-08 06:42:56 +00:00
|
|
|
|
SHELL=/bin/sh
|
|
|
|
|
PROGRAM=@DAEMON@
|
|
|
|
|
SYSCONFDIR=/etc/$PROGRAM
|
|
|
|
|
LOGDIR=/var/log/$PROGRAM
|
|
|
|
|
LOCALSTATEDIR=/var/lock
|
|
|
|
|
BINDIR=/usr/bin
|
2009-04-03 14:48:11 +00:00
|
|
|
|
CRONFILE=/etc/cron.d/@CRONFILE@
|
2008-12-08 06:42:56 +00:00
|
|
|
|
CNFFILE=$SYSCONFDIR/config.xml
|
|
|
|
|
LOCKFILE=$LOCALSTATEDIR/$PROGRAM
|
|
|
|
|
RETVAL=0
|
|
|
|
|
|
2014-11-28 16:47:02 +00:00
|
|
|
|
DEFAULT_NUMBER_OF_PROCESSES=1
|
2014-12-01 17:31:44 +00:00
|
|
|
|
NUMBER_OF_PROCESSES=$(xmlstarlet sel -t -v "/yandex/number_of_processes" $CNFFILE || echo $DEFAULT_NUMBER_OF_PROCESSES)
|
2014-11-28 16:47:02 +00:00
|
|
|
|
|
|
|
|
|
PIDDIR=/var/run/$PROGRAM
|
|
|
|
|
PIDFILE_PREFIX=$PIDDIR/$PROGRAM
|
2014-12-01 17:31:44 +00:00
|
|
|
|
PIDFILE_RE="$PIDFILE_PREFIX[0-9]*.pid"
|
2014-11-28 16:47:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generate_pid_name()
|
|
|
|
|
{
|
|
|
|
|
echo $PIDFILE_PREFIX$1.pid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generate_log_filename()
|
|
|
|
|
{
|
|
|
|
|
log_file=$(xmlstarlet sel -t -v "/yandex/logger/log" $CNFFILE)
|
|
|
|
|
# Не будем менять имя лог файла, при одном процессе
|
|
|
|
|
if [ $NUMBER_OF_PROCESSES -gt 1 ]; then
|
|
|
|
|
log_file=$(echo $log_file | sed "s/\.log/$1.log/")
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo $log_file
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
find_pid_files()
|
2008-12-08 06:42:56 +00:00
|
|
|
|
{
|
2014-11-28 16:47:02 +00:00
|
|
|
|
find $PIDDIR -regex $PIDFILE_RE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is_running()
|
|
|
|
|
{
|
|
|
|
|
pidfile=$1
|
|
|
|
|
|
|
|
|
|
[ -r "$pidfile" ] && pgrep -s $(cat "$pidfile") 1> /dev/null 2> /dev/null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
running_processes()
|
|
|
|
|
{
|
|
|
|
|
pidfiles=$(find_pid_files)
|
|
|
|
|
running=0
|
|
|
|
|
for pidfile in $pidfiles; do
|
|
|
|
|
if is_running $pidfile; then
|
|
|
|
|
running=$(($running + 1))
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
echo $running
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
any_runs()
|
|
|
|
|
{
|
|
|
|
|
if [[ $(running_processes) -gt 0 ]]; then return 0; else return 1; fi
|
2008-12-08 06:42:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wait4done()
|
|
|
|
|
{
|
2014-11-28 16:47:02 +00:00
|
|
|
|
while any_runs; do
|
2008-12-08 06:42:56 +00:00
|
|
|
|
sleep 1
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start()
|
|
|
|
|
{
|
2011-03-21 14:16:55 +00:00
|
|
|
|
[ -x $BINDIR/$PROGRAM ] || exit 0
|
2008-12-08 06:42:56 +00:00
|
|
|
|
local EXIT_STATUS
|
|
|
|
|
EXIT_STATUS=0
|
|
|
|
|
|
|
|
|
|
echo -n "Start $PROGRAM service: "
|
|
|
|
|
|
2010-08-05 14:06:05 +00:00
|
|
|
|
[ -f "$LOCKFILE" ] && sleep 1
|
2014-11-28 16:47:02 +00:00
|
|
|
|
if [[ $(running_processes) -eq $NUMBER_OF_PROCESSES ]]; then
|
2008-12-08 06:42:56 +00:00
|
|
|
|
echo -n "already running "
|
|
|
|
|
EXIT_STATUS=1
|
|
|
|
|
else
|
|
|
|
|
# Clean stale lock files
|
2014-11-28 16:47:02 +00:00
|
|
|
|
rm -f "$LOCKFILE"
|
2008-12-08 06:42:56 +00:00
|
|
|
|
mkdir -p $LOGDIR
|
|
|
|
|
mkdir -p $PIDDIR
|
|
|
|
|
chown -R $USER:$GROUP $LOGDIR
|
|
|
|
|
chown -R $USER:$GROUP $PIDDIR
|
2014-02-26 07:45:50 +00:00
|
|
|
|
chown -R $USER:$GROUP $SYSCONFDIR
|
2008-12-08 06:42:56 +00:00
|
|
|
|
|
2014-11-28 16:47:02 +00:00
|
|
|
|
for i in $(seq 1 $NUMBER_OF_PROCESSES); do
|
|
|
|
|
if ! is_running $(generate_pid_name $i); then
|
|
|
|
|
rm -f $(generate_pid_name $i)
|
|
|
|
|
# TODO может лучше передавать демону номер процесса, а демон сам будет делать все необходимые подстановки
|
|
|
|
|
su -l $USER -s $SHELL -c "\"$BINDIR/$PROGRAM\" --daemon --pid-file=\"$(generate_pid_name $i)\" --config-file=\"$CNFFILE\" --log-file=\"$(generate_log_filename $i)\""
|
|
|
|
|
EXIT_STATUS=$?
|
|
|
|
|
if [[ $EXIT_STATUS -ne 0 ]]; then
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
done
|
2008-12-08 06:42:56 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2014-11-28 16:47:02 +00:00
|
|
|
|
if [[ $EXIT_STATUS -eq 0 ]]; then
|
2008-12-08 06:42:56 +00:00
|
|
|
|
touch "$LOCKFILE"
|
|
|
|
|
echo "DONE"
|
|
|
|
|
else
|
|
|
|
|
echo "FAILED"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
return $EXIT_STATUS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stop()
|
|
|
|
|
{
|
|
|
|
|
local EXIT_STATUS
|
2008-12-18 11:07:42 +00:00
|
|
|
|
EXIT_STATUS=0
|
2008-12-08 06:42:56 +00:00
|
|
|
|
|
|
|
|
|
echo -n "Stop $PROGRAM service: "
|
|
|
|
|
|
2014-11-28 16:47:02 +00:00
|
|
|
|
if any_runs; then
|
2010-03-04 15:49:54 +00:00
|
|
|
|
if [ -f "$LOCKFILE" ]; then
|
2014-11-28 16:47:02 +00:00
|
|
|
|
for pid_file in $(find_pid_files); do
|
|
|
|
|
kill -TERM `cat "$pid_file"`
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
wait4done
|
|
|
|
|
rm -f "$LOCKFILE"
|
2010-03-04 15:49:54 +00:00
|
|
|
|
else
|
2014-11-28 16:47:02 +00:00
|
|
|
|
echo "has been stopping already"
|
|
|
|
|
return 1
|
2010-03-04 15:49:54 +00:00
|
|
|
|
fi
|
2010-08-02 17:29:22 +00:00
|
|
|
|
else
|
|
|
|
|
rm -f "$LOCKFILE"
|
2008-12-08 06:42:56 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "DONE"
|
|
|
|
|
return $EXIT_STATUS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
restart()
|
|
|
|
|
{
|
|
|
|
|
stop
|
|
|
|
|
start
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-09 19:01:07 +00:00
|
|
|
|
forcestop()
|
|
|
|
|
{
|
|
|
|
|
local EXIT_STATUS
|
|
|
|
|
EXIT_STATUS=0
|
|
|
|
|
|
|
|
|
|
echo -n "Stop $PROGRAM service: "
|
|
|
|
|
|
2014-11-28 16:47:02 +00:00
|
|
|
|
if any_runs; then
|
2010-03-09 19:01:07 +00:00
|
|
|
|
if [ -f "$LOCKFILE" ]; then
|
2014-11-28 16:47:02 +00:00
|
|
|
|
for pid_file in $(find_pid_files); do
|
|
|
|
|
kill -9 `cat "$pid_file"`
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
wait4done
|
2014-12-01 18:03:47 +00:00
|
|
|
|
rm -f "$LOCKFILE"
|
2010-03-09 19:01:07 +00:00
|
|
|
|
else
|
2014-11-28 16:47:02 +00:00
|
|
|
|
echo "has been stopping already"
|
|
|
|
|
return 1
|
2010-03-09 19:01:07 +00:00
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "DONE"
|
|
|
|
|
return $EXIT_STATUS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
forcerestart()
|
|
|
|
|
{
|
|
|
|
|
forcestop
|
|
|
|
|
start
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-03 14:48:11 +00:00
|
|
|
|
enable_cron()
|
|
|
|
|
{
|
|
|
|
|
sed -i 's/^#*//' "$CRONFILE"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disable_cron()
|
|
|
|
|
{
|
|
|
|
|
sed -i 's/^#*/#/' "$CRONFILE"
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-08 06:42:56 +00:00
|
|
|
|
# See how we were called.
|
|
|
|
|
EXIT_STATUS=0
|
|
|
|
|
case "$1" in
|
|
|
|
|
start)
|
2009-04-06 08:32:43 +00:00
|
|
|
|
start && enable_cron
|
2008-12-08 06:42:56 +00:00
|
|
|
|
;;
|
|
|
|
|
stop)
|
2009-04-03 14:48:11 +00:00
|
|
|
|
disable_cron && stop
|
2008-12-08 06:42:56 +00:00
|
|
|
|
;;
|
|
|
|
|
status)
|
2014-12-01 17:42:39 +00:00
|
|
|
|
if [[ $(running_processes) -eq $NUMBER_OF_PROCESSES ]]; then
|
|
|
|
|
echo "$PROGRAM service is running"
|
|
|
|
|
else
|
|
|
|
|
if [ ! -e $LOCKFILE ]; then
|
|
|
|
|
echo "$PROGRAM service is stopped";
|
|
|
|
|
else
|
2014-12-01 18:03:47 +00:00
|
|
|
|
echo "$PROGRAM: $(($NUMBER_OF_PROCESSES - $(running_processes))) of $NUMBER_OF_PROCESSES processes unexpectedly terminated"
|
2014-12-01 17:42:39 +00:00
|
|
|
|
fi
|
|
|
|
|
fi
|
2008-12-08 06:42:56 +00:00
|
|
|
|
;;
|
|
|
|
|
restart)
|
2009-04-10 13:21:55 +00:00
|
|
|
|
restart && enable_cron
|
2008-12-08 06:42:56 +00:00
|
|
|
|
;;
|
2010-03-09 19:01:07 +00:00
|
|
|
|
forcestop)
|
|
|
|
|
disable_cron && forcestop
|
|
|
|
|
;;
|
|
|
|
|
forcerestart)
|
|
|
|
|
forcerestart && enable_cron
|
|
|
|
|
;;
|
2008-12-08 06:42:56 +00:00
|
|
|
|
reload)
|
|
|
|
|
restart
|
|
|
|
|
;;
|
|
|
|
|
condstart)
|
2014-11-28 16:47:02 +00:00
|
|
|
|
any_runs || start
|
2008-12-08 06:42:56 +00:00
|
|
|
|
;;
|
|
|
|
|
condstop)
|
2014-11-28 16:47:02 +00:00
|
|
|
|
any_runs && stop
|
2008-12-08 06:42:56 +00:00
|
|
|
|
;;
|
|
|
|
|
condrestart)
|
2014-11-28 16:47:02 +00:00
|
|
|
|
any_runs && restart
|
2008-12-08 06:42:56 +00:00
|
|
|
|
;;
|
|
|
|
|
condreload)
|
2014-11-28 16:47:02 +00:00
|
|
|
|
any_runs && restart
|
2008-12-08 06:42:56 +00:00
|
|
|
|
;;
|
|
|
|
|
*)
|
2014-11-28 16:47:02 +00:00
|
|
|
|
echo "Usage: ${0##*/} {start|stop|status|restart|forcestop|forcerestart|reload|condstart|condstop|condrestart|condreload}"
|
2008-12-08 06:42:56 +00:00
|
|
|
|
EXIT_STATUS=2
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
exit $EXIT_STATUS
|