我对 bash 脚本非常陌生,我需要创建一个简单的脚本,每 5 分钟将日期和时间插入日志文件并保存。
我能做些什么来做到这一点?我可以从什么开始呢?
该date命令将输出当前日期和时间。您可以将命令的输出附加到带有>>. 最后,sleep将暂停脚本指定的秒数。
#!/bin/bash
while true; do
date >> /path/to/the/logfile/dates.log
sleep 300
done
Run Code Online (Sandbox Code Playgroud)
date如果无选项的输出不满足您的需要,该命令还提供了修改输出格式的选项。
或者,您可以发出
crontab -e
Run Code Online (Sandbox Code Playgroud)
并添加行
*/5 * * * * date >> /path/to/the/logfile/dates.log
Run Code Online (Sandbox Code Playgroud)
当然,您可以为日志文件使用任何名称,dates.log这只是我使用的占位符。