Команда «reboot» выводит меня из оболочки или принудительно завершает ее?

С date, shufиxargs:

Преобразование даты начала и окончания в «секунды с 1970 года -01 -01 00 :00 :00 UTC» и использование shufдля вывода шести случайных значений в этом диапазоне. Передайте этот результат в xargsи преобразуйте значения в нужный формат даты.

Редактировать:Если вы хотите, чтобы даты 2017 года были включены в выходные данные, вы должны добавить один год -1 с(2017-12-31 23:59:59)к дате окончания. shuf -iгенерирует случайные числа, включая начало и конец.

shuf -n6 -i$(date -d '1987-01-01' '+%s')-$(date -d '2017-01-01' '+%s')\
 | xargs -I{} date -d '@{}' '+%d/%m/%Y'

Пример вывода:

07/12/1988
22/04/2012
24/09/2012
27/08/2000
19/01/2008
21/10/1994
6
01.04.2021, 20:16
1 ответ

Это поведение, вероятно, будет иметь вариации в зависимости от дистрибутива/оболочки/конфигурации. В моей системе (Gentoo/SysVinit )вот что происходит, когда я запускаю перезагрузку (, вероятно, ваш сценарий 3):

Команда перезагрузки делегирует отключение (из man reboot):

.

If halt or reboot is called when the system is not in runlevel 0 or 6, in other words when it's running normally, shutdown will be invoked instead (with the -h or -r flag). For more info see the shutdown(8) manpage.

Все процессы (, включая оболочку ), получают SIGTERM, что дает 3 секунды на очистку (отman shutdown):

All processes are first notified that the system is going down by the signal SIGTERM. This gives programs like vi(1) the time to save the file being edited, mail and news processing programs a chance to exit cleanly, etc.

-t sec Tell init(8) to wait sec seconds between sending all processes the warning (SIGTERM) and the kill signal (SIGKILL), before changing to another run‐level. The default time, if no value is specified, between these two signals is three seconds. Warning: when shutdown calls init to perform the shutdown (the default behaviour), init checks to see if all processes have terminated and will stop waiting early once its children have all termi‐ nated. When shutdown is called with the -n flag, it waits the full time specified (or three seconds) even if all other processes have terminated.

Bash фактически игнорирует SIGTERM (изman bash):

When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not kill an interactive shell)

Итак, initменяет уровень запуска, (возможно, )отправляет еще один SIGTERM, а затем SIGKILL (изman init):

When init is requested to change the runlevel, it sends the warning signal SIGTERM to all processes that are undefined in the new runlevel. It then waits 3 seconds before forcibly terminating these processes via the SIGKILL signal.

TLDR; bash завершится без корректного завершения работы, другие программы могут, но я бы не стал полагаться на какое-либо конкретное поведение.

0
28.04.2021, 22:54

Теги

Похожие вопросы