2014-12-09 17:17:45 +00:00
|
|
|
|
#!/bin/bash
|
2008-12-08 06:42:56 +00:00
|
|
|
|
### 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
|
2014-12-11 16:55:25 +00:00
|
|
|
|
SHELL=/bin/bash
|
2008-12-08 06:42:56 +00:00
|
|
|
|
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-09 12:44:50 +00:00
|
|
|
|
NUMBER_OF_PROCESSES=$DEFAULT_NUMBER_OF_PROCESSES
|
|
|
|
|
|
|
|
|
|
LOG_FILE=""
|
|
|
|
|
|
|
|
|
|
# С помощью xmlstarlet пытаемся взять некоторые параметры из конфига
|
|
|
|
|
if command -v xmlstarlet >/dev/null 2>&1; then
|
|
|
|
|
NUMBER_OF_PROCESSES=$(xmlstarlet sel -t -v "/yandex/number_of_processes" $CNFFILE || echo $DEFAULT_NUMBER_OF_PROCESSES)
|
|
|
|
|
LOG_FILE=$(xmlstarlet sel -t -v "/yandex/logger/log" $CNFFILE || echo $LOG_FILE)
|
|
|
|
|
fi
|
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
|
|
|
|
|
2015-03-25 11:36:46 +00:00
|
|
|
|
SUPPORTED_COMMANDS="{start|stop|status|restart|forcestop|forcerestart|reload|condstart|condstop|condrestart|condreload}"
|
|
|
|
|
is_supported_command()
|
|
|
|
|
{
|
|
|
|
|
echo $SUPPORTED_COMMANDS | grep -E "(\{|\|)$1(\||})" &> /dev/null
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 12:09:00 +00:00
|
|
|
|
generate_program_name()
|
|
|
|
|
{
|
|
|
|
|
if [ $NUMBER_OF_PROCESSES -eq 1 ]; then
|
|
|
|
|
echo $PROGRAM
|
|
|
|
|
else
|
2014-12-09 14:10:46 +00:00
|
|
|
|
echo $PROGRAM$1
|
2014-12-03 12:09:00 +00:00
|
|
|
|
fi
|
|
|
|
|
}
|
2014-11-28 16:47:02 +00:00
|
|
|
|
|
|
|
|
|
generate_pid_name()
|
|
|
|
|
{
|
2014-12-09 13:17:38 +00:00
|
|
|
|
if [ $NUMBER_OF_PROCESSES -gt 1 ]; then
|
|
|
|
|
echo $PIDFILE_PREFIX$1.pid
|
|
|
|
|
else
|
|
|
|
|
echo $PIDFILE_PREFIX.pid
|
|
|
|
|
fi
|
2014-11-28 16:47:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-09 12:44:50 +00:00
|
|
|
|
specific_log_file_for_each_process()
|
2014-11-28 16:47:02 +00:00
|
|
|
|
{
|
|
|
|
|
# Не будем менять имя лог файла, при одном процессе
|
2014-12-09 12:44:50 +00:00
|
|
|
|
# Не выставляем, если имя лог файла пустое
|
|
|
|
|
if [ $NUMBER_OF_PROCESSES -gt 1 ] && [ "$LOG_FILE" != "" ]; then
|
|
|
|
|
log_file=$(echo $LOG_FILE | sed "s/\.log/$1.log/")
|
|
|
|
|
echo "--log-file=\"$log_file\""
|
2014-11-28 16:47:02 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2014-12-09 12:44:50 +00:00
|
|
|
|
echo ""
|
2014-11-28 16:47:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
find_pid_files()
|
2008-12-08 06:42:56 +00:00
|
|
|
|
{
|
2015-03-25 15:49:11 +00:00
|
|
|
|
[[ -e $PIDDIR ]] && find $PIDDIR -regex "$PIDFILE_RE"
|
2014-11-28 16:47:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 11:35:14 +00:00
|
|
|
|
all_runs()
|
|
|
|
|
{
|
|
|
|
|
[[ $(running_processes) -eq $NUMBER_OF_PROCESSES ]]
|
|
|
|
|
}
|
|
|
|
|
|
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: "
|
|
|
|
|
|
2015-02-10 11:35:14 +00:00
|
|
|
|
if all_runs; then
|
2008-12-08 06:42:56 +00:00
|
|
|
|
echo -n "already running "
|
|
|
|
|
EXIT_STATUS=1
|
|
|
|
|
else
|
|
|
|
|
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)
|
2014-12-29 18:52:50 +00:00
|
|
|
|
# чтобы лок не удерживался в течении времени жизни дочернего процесса, освободим лок
|
|
|
|
|
su -l $USER -s $SHELL -c "flock -u 9; exec -a $(generate_program_name $i) \"$BINDIR/$PROGRAM\" --daemon --pid-file=\"$(generate_pid_name $i)\" --config-file=\"$CNFFILE\" $(specific_log_file_for_each_process $i)"
|
2014-11-28 16:47:02 +00:00
|
|
|
|
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
|
|
|
|
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-12-29 17:31:30 +00:00
|
|
|
|
for pid_file in $(find_pid_files); do
|
|
|
|
|
kill -TERM `cat "$pid_file"`
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
wait4done
|
2008-12-08 06:42:56 +00:00
|
|
|
|
|
|
|
|
|
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-12-29 17:31:30 +00:00
|
|
|
|
for pid_file in $(find_pid_files); do
|
|
|
|
|
kill -9 `cat "$pid_file"`
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
wait4done
|
2010-03-09 19:01:07 +00:00
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-29 17:31:30 +00:00
|
|
|
|
is_cron_disabled()
|
|
|
|
|
{
|
|
|
|
|
[[ `grep -E "^#.*" $CRONFILE` == `cat $CRONFILE` ]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main()
|
|
|
|
|
{
|
|
|
|
|
# See how we were called.
|
|
|
|
|
EXIT_STATUS=0
|
|
|
|
|
case "$1" in
|
2008-12-08 06:42:56 +00:00
|
|
|
|
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
|
|
|
|
;;
|
|
|
|
|
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)
|
2015-02-10 11:35:14 +00:00
|
|
|
|
all_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-12-29 17:31:30 +00:00
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
exit $EXIT_STATUS
|
|
|
|
|
}
|
2008-12-08 06:42:56 +00:00
|
|
|
|
|
2015-03-24 15:28:02 +00:00
|
|
|
|
status()
|
|
|
|
|
{
|
|
|
|
|
if [[ $(running_processes) -eq $NUMBER_OF_PROCESSES ]]; then
|
|
|
|
|
echo "$PROGRAM service is running"
|
|
|
|
|
else
|
|
|
|
|
if is_cron_disabled; then
|
|
|
|
|
echo "$PROGRAM service is stopped";
|
|
|
|
|
else
|
|
|
|
|
echo "$PROGRAM: $(($NUMBER_OF_PROCESSES - $(running_processes))) of $NUMBER_OF_PROCESSES processes unexpectedly terminated"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 11:36:46 +00:00
|
|
|
|
# выполняем команды, не нуждающиеся в блокировке
|
|
|
|
|
if ! is_supported_command "$1"; then
|
|
|
|
|
echo "Usage: ${0##*/} $SUPPORTED_COMMANDS"
|
|
|
|
|
exit 2
|
|
|
|
|
fi
|
|
|
|
|
|
2015-03-24 15:28:02 +00:00
|
|
|
|
if [[ "$1" == "status" ]]; then
|
|
|
|
|
status
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
2015-03-25 11:36:46 +00:00
|
|
|
|
|
2014-12-29 17:31:30 +00:00
|
|
|
|
(
|
2014-12-29 18:52:50 +00:00
|
|
|
|
if flock -n 9; then
|
|
|
|
|
main "$@"
|
|
|
|
|
else
|
|
|
|
|
echo "Init script is already running" && exit 1
|
|
|
|
|
fi
|
2014-12-29 17:31:30 +00:00
|
|
|
|
) 9> $LOCKFILE
|