linux - cron 任务不写入文件

标签 linux unix cron

我正在尝试使用 cron 每分钟将当前日期和时间附加到日志文件中。我希望以特定方式设置日期和时间的格式。

这个有效:

* * * * * date >> /home/user/time1.txt

这不是:

* * * * * date +%Y%m%d%H%M%S >> /home/user/time2.txt

非常感谢任何见解!

最佳答案

问题是 cron% 视为换行符。你需要逃避他们

来自 crontab POSIX man 页面:

Percent-signs (%) in the command, unless escaped with backslash \, will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

* * * * * date +\%Y\%m\%d\%H\%M\%S >> /home/user/time2.txt

关于linux - cron 任务不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47975137/

相关文章:

linux - 您能否在受控网络中优化/配置 TCP,使其变得与 UDP 一样快?

c++ - 在静态初始化程序中使用 getenv() 是否安全,即在 main() 之前?

linux - Unix 到 Linux 的迁移 - unix2dos

unix - cron 作业每分钟运行一次,从半小时开始

php - 你能在 PHP cron 作业中运行数据库查询吗?

c - 系统调用写入

python - 使用比 Linux 服务器上安装的版本更新的 Python 版本

linux - 如何记录每个命令的持续时间/结果并用 `history` 显示此信息?

windows - 如何在 GForth (GNU Forth) 的文件名中包含空格?

python - 如果没有使用简单的 cron 作业运行/停止/错误,请重新启动 python 脚本