如何使用当前时间的日期更新文件名

时间:2020-12-04 19:24:35

I am trying to launch a stdbuf which be able to make a new log each day using this order in the shell:

我正在尝试启动一个stdbuf,它能够在shell中使用此命令每天创建一个新日志:

stdbuf -i0 -o0 -e0 /.../my_dir/watchdog_foo >> /.../my_dir/log_foo_`date '+%F'` &

With this, I would want to have:

有了这个,我想要:

log_foo_2017-11-28
log_foo_2017-11-29
...

For testing, I have launched this other order:

为了测试,我已经启动了这个其他订单:

stdbuf -i0 -o0 -e0 /.../my_dir/watchdog_foo >> /.../my_dir/log_foo_`date '+%R'` &

trying to see if a log was created each minute, but it didn't happened. The log had been writing on the same file all this time whileas what I hopped was:

试图查看每分钟是否创建了一个日志,但它没有发生。日志一直写在同一个文件上,而我跳的是:

log_foo_18:05
log_foo_18:06
...

How could I modify this command for achieve my purpose?

如何修改此命令以实现我的目的?

1 个解决方案

#1


0  

When you launch this command, the date +%R is expanded by your shell to the current time. After that, it is not updated again, so your log will be redirected to the same file. To get your desired behavior, you'd need to re-launch your script every day (to get the updated date). You could use a tool like cron to run the script every night at midnight to kill the previous process and re-run with the new date.

启动此命令时,shell将日期+%R扩展为当前时间。之后,它不会再次更新,因此您的日志将被重定向到同一文件。要获得所需的行为,您需要每天重新启动脚本(以获取更新的日期)。你可以使用像cron这样的工具每晚午夜运行脚本来杀死上一个进程并重新运行新日期。

You could also look into leveraging a tool like logrotate.

您还可以考虑利用像logrotate这样的工具。

#1


0  

When you launch this command, the date +%R is expanded by your shell to the current time. After that, it is not updated again, so your log will be redirected to the same file. To get your desired behavior, you'd need to re-launch your script every day (to get the updated date). You could use a tool like cron to run the script every night at midnight to kill the previous process and re-run with the new date.

启动此命令时,shell将日期+%R扩展为当前时间。之后,它不会再次更新,因此您的日志将被重定向到同一文件。要获得所需的行为,您需要每天重新启动脚本(以获取更新的日期)。你可以使用像cron这样的工具每晚午夜运行脚本来杀死上一个进程并重新运行新日期。

You could also look into leveraging a tool like logrotate.

您还可以考虑利用像logrotate这样的工具。