UNIX crontab中的日期时间格式 [英] Date time format in UNIX crontab

查看:280
本文介绍了UNIX crontab中的日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每6小时运行一次cron来备份我的数据库。
我想要文件名包含日期&时间它是按以下格式创建的:

I'm running a cron every 6 hours to backup my database. I want the filename to contain the date & time it was created in the following format:

mysqlbackup_22/5/2013_15:45.sql.gz

这是我运行的命令:

date=`date -d`; mysqldump -uusername -ppassword dbname | gzip > /path/to/dir/mysqlbackup_$date.sql.gz

我需要更改 date -d 到?

推荐答案

如果使用日期格式+%d-%m-%Y_%H:%M,你可能需要用反斜杠转义%字符,例如:date +\%d-\%m-\%

If you use a date format like date +"%d-%m-%Y_%H:%M" in your crontab you may need to escape the % characters with a backslash, like this: date +"\%d-\%m-\%Y_\%H:\%M".

许多cron通过将其替换为换行并将以下文本作为stdin发送到命令之前,特别处理%详情请参阅man 5 crontab。

Many crons handle % specially by replacing them with newline and sending the following text as stdin to the command before it, see man 5 crontab for details.

这篇关于UNIX crontab中的日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆