import os
import sys
import time
def main():
logfile = "updownlog.csv"
write_header = False
if len(sys.argv) != 2:
sys.exit("Error: script takes exactly one argument")
if sys.argv[1] != "shutdown" and sys.argv[1] != "startup":
sys.exit("Error: First argument should be 'startup' or 'shutdown'")
typ = sys.argv[1]
if not os.path.exists(logfile):
write_header = True
with open("updownlog.csv", "a") as f:
now = time.time()
if write_header:
f.write("type,timestamp\n")
f.write("{},{}\n".format(typ, now))
if __name__ == "__main__":
main()
接下来,您需要创建触发此脚本的系统服务。我无耻地抄袭了this answer on UnixSX中提供的解决方案:全部归功于“john9631”!如果您仍然使用基于init.d的系统,那么在这个线程中也有很好的答案。你知道吗
因此,为日志创建服务文件:
vim /etc/systemd/system/log_start_stop.service
并在文件内容中复制:
[Unit]
Description=Log startup and shutdown times
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart="/home/Sungod3k/log.py startup"
ExecStop="/home/Sungod3k/log.py shutdown"
[Install]
WantedBy=multi-user.target
如果您使用的是Linux(这里我假设为Systemd),那么您可以编写一个在启动/关闭时执行代码的服务。该代码会将当前时间戳写入CSV文件,并带有“startup”或“shutdown”指示符。你知道吗
下面是一个Python3脚本,它的第一个参数是要记录到的时间戳类型“updownlog.txt文件“:”
接下来,您需要创建触发此脚本的系统服务。我无耻地抄袭了this answer on UnixSX中提供的解决方案:全部归功于“john9631”!如果您仍然使用基于init.d的系统,那么在这个线程中也有很好的答案。你知道吗
因此,为日志创建服务文件:
并在文件内容中复制:
然后使用以下命令启用服务:
当然,这还不能告诉你你是否有睡眠不足,所以你需要做一些后处理,例如用Python或R,甚至
awk
。你知道吗相关问题 更多 >
编程相关推荐