如何计算 shutdown -r +${MINUTES} 的最大值?

如何计算 shutdown -r +${MINUTES} 的最大值?

我使用 CentOS 7.5.1804 和 GNU bash 版本 4.2.46(2)-release (x86_64-redhat-linux-gnu)。

对于维护时段内的计划重新启动,我计算从现在到下次重新启动的分钟数。例如,要在大约 3 个月内重新启动,我会使用

shutdown -r +129600

由于有兴趣更好地了解 bash,我很好奇最高值是什么以及如何计算它?

使用 int32 进行的计划关闭将报告

shutdown -r +4294967295
Shutdown scheduled for Tue 10184-07-27 ... 

更高的值仍然是可能的,但是如何计算可能的最大值?

答案1

在我的机器上,/sbin/shutdown 是 /sbin/systemctl 的符号链接。
在 systemctl 源中探究(https://code.launchpad.net/~ubuntu-branches/ubuntu/trusty/systemd/trusty), 我懂了

  • shutdown_parse_argv函数将时间规范解析为名为的变量,arg_when其类型为usec_t
  • send_shutdownd函数sd_shutdown_command使用该 usec_t 值创建一个结构体
  • sd_shutdown_command结构体包含:

    /* Microseconds after the epoch 1970 UTC */
    uint64_t usec;
    

所以,最长时间似乎是:

$ \bc <<< '(2^63-1) / 10^6 / 60' # minutes
153722867280
$ \bc <<< '(2^63-1) / 10^6 / 60 / 24 / 365' # years
17548272

话又说回来,我没读过parse_time_spec函数来查看参数实际上如何解析为时间值。

相关内容