自 systemd 服务启动以来的时间 - Bash
Time since the systemd service was started - Bash
我想知道自 systemd
服务启动以来的持续时间,使用 --property=ActiveEnterTimestamp
我可以看到它启动的时间,我想将它与当前时间进行比较在几秒或几分钟内得到一个值,我怎样才能用 bash
实现这个
如果我使用 解决方案,我得到一个字符串,但实际上我无法获得任何时间对象来做出决定,我们将不胜感激。
您可以使用 GNU date 将 ActiveEnterTimestamp 值转换为 seconds-since-the-epoch,然后减去当前 seconds-since-the-epoch 得到 运行 秒数。
servicestartsec=$(date -d "$(systemctl show --property=ActiveEnterTimestamp your-service-here | cut -d= -f2)" +%s)
serviceelapsedsec=$(( $(date +%s) - servicestartsec))
将“your-service-here”替换为您的实际服务名称。
第一行通过提取 systemctl show --property=ActiveEnterTimestamp...
的日期部分(使用 cut
提取第二个 =
分隔字段)然后将其传递给GNU date
并要求在 seconds-since-the-epoch.
中输出
第二行简单地从当前时间中减去开始时间以获得以秒为单位的经过时间。根据需要将其除以得到经过的分钟数、小时数等。
我想知道自 systemd
服务启动以来的持续时间,使用 --property=ActiveEnterTimestamp
我可以看到它启动的时间,我想将它与当前时间进行比较在几秒或几分钟内得到一个值,我怎样才能用 bash
如果我使用
您可以使用 GNU date 将 ActiveEnterTimestamp 值转换为 seconds-since-the-epoch,然后减去当前 seconds-since-the-epoch 得到 运行 秒数。
servicestartsec=$(date -d "$(systemctl show --property=ActiveEnterTimestamp your-service-here | cut -d= -f2)" +%s)
serviceelapsedsec=$(( $(date +%s) - servicestartsec))
将“your-service-here”替换为您的实际服务名称。
第一行通过提取 systemctl show --property=ActiveEnterTimestamp...
的日期部分(使用 cut
提取第二个 =
分隔字段)然后将其传递给GNU date
并要求在 seconds-since-the-epoch.
第二行简单地从当前时间中减去开始时间以获得以秒为单位的经过时间。根据需要将其除以得到经过的分钟数、小时数等。