Есть ли альтернатива этому?
Это довольно простая задача linux, которую можно решить с помощью timeout
или timelimit
двоичный.
$ timeout -k 1 5 bash -c "while true ; do sleep 1 && echo 1 ; done" || echo
# the above task will be terminated in 5 secs or killed in 6 secs.
# timeout [OPTION] DURATION COMMAND [ARG]...
# -k, --kill-after=DURATION also send a KILL signal if COMMAND is still running
this long after the initial signal was sent (not available on some containers)
# DURATION is a floating point number with an optional suffix: 's' for
# seconds (the default), 'm' for minutes, 'h' for hours or 'd' for
# days. A duration of 0 disables the associated timeout.
# the above task will be terminated in 5 secs or killed in 6 secs.
$ timelimit -t 5 -T 1 bash -c "while true ; do sleep 1 && echo 1 ; done" || exit 0
# the above task will be terminated in 5 secs or killed in 6 secs.
# -t 5 - five seconds before sending SIGTERM to the process
# -T 1 - one second after SIGTERM was sent and before sending SIGKILL
Просто укажите это в команде InitContainer
, чтобы остановить задачу через некоторое разумное время.
Чтобы InitContainer
думал, что команда завершена успешно, даже если она была завершена, добавьте || echo
или || exit 0
в конец команды, как показано в примере выше.
timeout
присутствует в системе по умолчанию (отмечено на busybox, ubuntu, centos, alpine), но timelimit
отсутствует и может быть установлен соответствующим диспетчером пакетов. Например:
$ apt-cache search timelimit
timelimit - simple utility to limit a process's absolute execution time
$ apt-get -y install timelimit
Вы можете найти другие способы убить процесс, используя только команды bash: